class Base
class DerivedLevel1
class DerivedLevel2
1: /** inherit7.cpp */
3: #include <iostream>
4: using namespace std;
6: class Base
7: {
8: public:
9: virtual void display()
10: {
11: cout << "\nUsing Base version of display().";
12: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
13: }
14: };
16: class DerivedLevel1 : public Base
17: {
18: public:
19: void display()
20: {
21: cout << "\nUsing DerivedLevel1 version of display().";
22: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
23: }
24: };
26: //DerivedLevel2 inherits DerivedLevel1.
27: class DerivedLevel2 : public DerivedLevel1
28: {
29: public:
30: void display()
31: {
32: cout << "\nUsing DerivedLevel2 version of display().";
33: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
34: }
35: };
37: int main()
38: {
39: cout << "\nThis program illustrates that virtual functions retain "
40: "their virtual nature\nwhen inherited, even without repetition "
41: "of the \"virtual\" keyword modifier for\nthe overridden function "
42: "in a derived class.";
43: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
45: Base *p;
46: Base baseObject;
47: DerivedLevel1 derived1Object;
48: DerivedLevel2 derived2Object;
50: p = &baseObject;
51: p->display(); //Use Base's display()
53: p = &derived1Object;
54: p->display(); //Use DerivedLevel1's display()
56: p = &derived2Object;
57: p->display(); //Use DerivedLevel2's display()
58: }