Polymorphism 469
14
Listing 14.6 rewrites Listing 14.5 to take advantage of virtual derivation.
LISTING14.6 Illustration of the Use of Virtual Inheritance
0: // Listing 14.6
1: // Virtual inheritance
2: #include <iostream>
3: using namespace std;
4:
5: typedef int HANDS;
6: enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;
7:
8: class Animal // common base to both horse and bird
9: {
10: public:
11: Animal(int);
12: virtual ~Animal() { cout << “Animal destructor...\n”; }
13: virtual int GetAge() const { return itsAge; }
14: virtual void SetAge(int age) { itsAge = age; }
15: private:
16: int itsAge;
17: };
18:
19: Animal::Animal(int age):
20: itsAge(age)
21: {
22: cout << “Animal constructor...\n”;
23: }
24:
25: class Horse : virtual public Animal
FIGURE14.3
A diamond
inheritance. Animal
Horse Bird
Pegasus