Answer to Question #295731 in C++ for Harshi

Question #295731

   A class has an integer data member ‘I’ and a function named ‘printNum’ to print the value of ‘I’. Its subclass also has an integer data member ‘j’ and a function named ‘printNum’ to print the value of ‘j’. Make an object of the subclass and use it to assign a value to ‘I’ and to ‘j’. Now call the function ‘printNum’ by this object.




1
Expert's answer
2022-02-09T13:07:38-0500
#include <iostream>
#include <string>
using namespace std;






class IntegerI{
private:
	int i;
public:
	IntegerI(int i){
		this->i=i;
	}


	void printNum(){
		cout<<"i = "<<i<<"\n";
	}
};


class IntegerJ:public IntegerI{
private:
	int j;
public:
	IntegerJ(int i,int j):IntegerI(i){
		this->j=j;
	}


	void printNum(){
		IntegerI::printNum();
		cout<<"j = "<<j<<"\n";
	}
};


int main() {
	IntegerJ integerJ(5,6);
	integerJ.printNum();




	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog