Programming Tutorials

virtual inheritance example in C++

By: Priya in C++ Tutorials on 2007-09-17  

It is possible to tell C++ that you do not want two copies of the shared base class, but rather to have a single shared base class, as shown in Figure below.

You accomplish this by making Animal a virtual base class of both Horse and Bird. The Animal class does not change at all. The Horse and Bird classes change only in their use of the term virtual in their declarations. Pegasus, however, changes substantially.

Normally, a class's constructor initializes only its own variables and its base class. Virtually inherited base classes are an exception, however. They are initialized by their most derived class. Thus, Animal is initialized not by Horse and Bird, but by Pegasus. Horse and Bird have to initialize Animal in their constructors, but these initializations will be ignored when a Pegasus object is created.

A diamond inheritance.

Illustration of the use of virtual inheritance.

1:     // 
2:     // Virtual inheritance
3:     #include <iostream.h>
4:
5:     typedef int HANDS;
6:     enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;
7:     enum BOOL { FALSE, TRUE };
8:
9:     class Animal        // common base to both horse and bird
10:    {
11:    public:
12:       Animal(int);
13:       virtual ~Animal() { cout << "Animal destructor...\n"; }
14:       virtual int GetAge() const { return itsAge; }
15:       virtual void SetAge(int age) { itsAge = age; }
16:    private:
17:       int itsAge;
18:    };
19:
20:    Animal::Animal(int age):
21:    itsAge(age)
22:    {
23:       cout << "Animal constructor...\n";
24:    }
25:
26:    class Horse : virtual public Animal
27:    {
28:    public:
29:       Horse(COLOR color, HANDS height, int age);
30:       virtual ~Horse() { cout << "Horse destructor...\n"; }
31:       virtual void Whinny()const { cout << "Whinny!... "; }
32:       virtual HANDS GetHeight() const { return itsHeight; }
33:       virtual COLOR GetColor() const { return itsColor; }
34:    protected:
35:       HANDS itsHeight;
36:       COLOR itsColor;
37:    };
38: 
39:    Horse::Horse(COLOR color, HANDS height, int age):
40:       Animal(age),
41:       itsColor(color),itsHeight(height)
42:    {
43:       cout << "Horse constructor...\n";
44:    }
45:
46:    class Bird : virtual public Animal
47:    {
48:    public:
49:       Bird(COLOR color, BOOL migrates, int age);
50:       virtual ~Bird() {cout << "Bird destructor...\n";  }
51:       virtual void Chirp()const { cout << "Chirp... ";  }
52:       virtual void Fly()const 
53:          { cout << "I can fly! I can fly! I can fly! "; }
54:       virtual COLOR GetColor()const { return itsColor; }
55:       virtual BOOL GetMigration() const { return itsMigration; }
56:    protected:
57:       COLOR itsColor;
58:       BOOL itsMigration;
59:    };
60:
61:    Bird::Bird(COLOR color, BOOL migrates, int age):
62:       Animal(age),
63:       itsColor(color), itsMigration(migrates)
64:    {
65:       cout << "Bird constructor...\n";
66:    }
67:
68:    class Pegasus : public Horse, public Bird
69:    {
70:    public:
71:       void Chirp()const { Whinny(); }
72:       Pegasus(COLOR, HANDS, BOOL, long, int);
73:       ~Pegasus() {cout << "Pegasus destructor...\n";}
74:       virtual long GetNumberBelievers() const 
75:          { return  itsNumberBelievers; }
76:       virtual COLOR GetColor()const { return Horse::itsColor; }
77:    private:
78:       long itsNumberBelievers;
79:    };
80: 
81:    Pegasus::Pegasus(
82:       COLOR aColor,
83:       HANDS height,
84:       BOOL migrates,
85:       long NumBelieve,
86:       int age):
87:    Horse(aColor, height,age),
88:    Bird(aColor, migrates,age),
89:    Animal(age*2),
90:    itsNumberBelievers(NumBelieve)
91:    {
92:       cout << "Pegasus constructor...\n";
93:    }
94:
95:    int main()
96:    {
97:       Pegasus *pPeg = new Pegasus(Red, 5, TRUE, 10, 2);
98:       int age = pPeg->GetAge();
99:       cout << "This pegasus is " << age << " years old.\n";
100:      delete pPeg;
101:     return 0;
102: }

Output: Animal constructor...
Horse constructor...
Bird constructor...
Pegasus constructor...
This pegasus is 4 years old.
Pegasus destructor...
Bird destructor...
Horse destructor...
Animal destructor... 

Analysis: On line 26, Horse declares that it inherits virtually from Animal, and on line 46, Bird makes the same declaration. Note that the constructors for both Bird and Animal still initialize the Animal object.

Pegasus inherits from both Bird and Animal, and as the most derived object of Animal, it also initializes Animal. It is Pegasus' initialization which is called, however, and the calls to Animal's constructor in Bird and Horse are ignored. You can see this because the value 2 is passed in, and Horse and Bird pass it along to Animal, but Pegasus doubles it. The result, 4, is reflected in the printout on line 99 and as shown in the output.

Pegasus no longer has to disambiguate the call to GetAge(), and so is free to simply inherit this function from Animal. Note that Pegasus must still disambiguate the call to GetColor(), as this function is in both of its base classes and not in Animal.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in C++ )

Latest Articles (in C++)