Question 6. Inheritance [9]
Carefully study the program below then show the precise output it would produce.
#include <iostream>
using namespace std;
class grandparent {
protected: int gpVal;
public:
grandparent() {
gpVal = 1;
cout << "setting gpVal " << gpVal << endl;
}
virtual void print() = 0;
};
class parent: public grandparent {
protected: int pVal;
public:
parent(int i = 10) {
pVal = i;
cout << "setting pVal " << pVal << endl;
}
virtual void print() {
cout << "parent " << pVal << "," << gpVal << "\n";
}
};
class child: public parent {
protected: int cVal;
public:
child(int i = 100) {
cVal = i;
cout << "setting cVal " << cVal << endl;
}
virtual void print() {
cout << "child " << cVal << "," << pVal << "," << gpVal << "\n";
}
};
int main() {
grandparent *gp;
parent *p1;
parent p2;
child *c1;
p1 = new child(100);
c1 = new child(200);
gp = new parent(10);
gp->print();
p1->print();
c1->print();
}