class baseClass
{
public:
void print();
int getX();
baseClass(int a = 0);
protected:
int x;
};
class derivedClass: public baseClass
{
public:
void print();
int getResult();
derivedClass(int a = 0, int b = 0);
private:
int y;
};
void baseClass::print()
{
cout<<"In base: x= "<< x <<endl;
}
baseClass::baseClass(int a)
{
x = a;
}
int baseClass::getX()
{
return x;
}
void derivedClass::print()
{
cout<<"In derived: x= "<< x <<", y= "<< y
<<"; x + y = "<< x + y <<endl;
}
int derivedClass::getResult()
{
return x + y;
}
derivedClass::derivedClass(int a, int b): baseClass(a)
{
y = b;
}
What is the output of the following function main?
int main()
{
baseClass baseObject(7);
derivedClass derivedObject(3, 8);
baseObject.print();
derivedObject.print();
cout<<"****"<<baseObject.getX()<<endl;
cout<<"####"<<derivedObject.getResult()<<endl;
return 0;
}
The output the function main is:
In base: x= 7
In derived: x= 3, y= 8; x + y = 11
****7
####11
Comments
Leave a comment