Suppose that you have the following classes, classA and classB (10 points): /***
ID: 3551095 • Letter: S
Question
Suppose that you have the following classes, classA and classB (10 points):
/**** Declaration of Class A****/
class classA
{
public:
void print() const;
void doubleNum();
classA(int a = 0);
protected:
int x;
}
/**** Definition of Class A****/
void classA::print() const
{
cout<< "ClassA x: " << x << endl;
}
void classA:: doubleNum ()
{
x = 2 * x;
}
classA::classA (int a)
{
x = a;
}
/**** Declaration of Class B****/
class classB: public classA
{
public:
void print() const;
void doubleNum();
classB (int a = 0, int b = 0);
protected:
int y;
}
/**** Definition of Class B****/
void classB::print() const
{
cout<<"ClassB y:" << y <<endl;
}
void classB::doubleNum()
{
y = 2 * y;
x= 2*x;
}
classB::classB(int a, int b)
{
x = a;
y = b;
}
What is the output of the following function main?
int main()
{
classA*ptrA;
classA ObjectA(2);
classB ObjectB(3,5);
ptrA = &ObjectA;
ptrA->doubleNum();
ptrA->print(); // give the output
cout << endl;
ptrA = &ObjectB;
ptrA->doubleNum();
ptrA->print(); // give the output
cout << endl;
return 0;
}
Output:
What is the output of the above function main (), if the definition of classA is replaced by the following definition?
class classA
{
public:
virtual void print() const;
virtual void doubleNum();
classA(int a = 0);
protected:
int x;
}
Output:
Explanation / Answer
1st case output:
ClassA x; 4
ClassA x: 6
2nd case output:
ClassA x: 4
ClassB y: 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.