Multiple inheritance example in C++
By: Norman Chap
It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. To derive from more than the base class, you separate each base class by commas in the class designation. Listing below illustrates how to declare Pegasus so that it derives from both Horses and Birds. The program then adds Pegasus objects to both types of lists.
Multiple inheritance.
1: // Multiple inheritance. 2: // Multiple Inheritance 3: 4: #include <iostream.h> 5: 6: class Horse 7: { 8: public: 9: Horse() { cout << "Horse constructor... "; } 10: virtual ~Horse() { cout << "Horse destructor... "; } 11: virtual void Whinny() const { cout << "Whinny!... "; } 12: private: 13: int itsAge; 14: }; 15: 16: class Bird 17: { 18: public: 19: Bird() { cout << "Bird constructor... "; } 20: virtual ~Bird() { cout << "Bird destructor... "; } 21: virtual void Chirp() const { cout << "Chirp... "; } 22: virtual void Fly() const 23: { 24: cout << "I can fly! I can fly! I can fly! "; 25: } 26: private: 27: int itsWeight; 28: }; 29: 30: class Pegasus : public Horse, public Bird 31: { 32: public: 33: void Chirp() const { Whinny(); } 34: Pegasus() { cout << "Pegasus constructor... "; } 35: ~Pegasus() { cout << "Pegasus destructor... "; } 36: }; 37: 38: const int MagicNumber = 2; 39: int main() 40: { 41: Horse* Ranch[MagicNumber]; 42: Bird* Aviary[MagicNumber]; 43: Horse * pHorse; 44: Bird * pBird; 45: int choice,i; 46: for (i=0; i<MagicNumber; i++) 47: { 48: cout << "\n(1)Horse (2)Pegasus: "; 49: cin >> choice; 50: if (choice == 2) 51: pHorse = new Pegasus; 52: else 53: pHorse = new Horse; 54: Ranch[i] = pHorse; 55: } 56: for (i=0; i<MagicNumber; i++) 57: { 58: cout << "\n(1)Bird (2)Pegasus: "; 59: cin >> choice; 60: if (choice == 2) 61: pBird = new Pegasus; 62: else 63: pBird = new Bird; 64: Aviary[i] = pBird; 65: } 66: 67: cout << "\n"; 68: for (i=0; i<MagicNumber; i++) 69: { 70: cout << "\nRanch[" << i << "]: " ; 71: Ranch[i]->Whinny(); 72: delete Ranch[i]; 73: } 74: 75: for (i=0; i<MagicNumber; i++) 76: { 77: cout << "\nAviary[" << i << "]: " ; 78: Aviary[i]->Chirp(); 79: Aviary[i]->Fly(); 80: delete Aviary[i]; 81: } 82: return 0; 83: } Output: (1)Horse (2)Pegasus: 1 Horse constructor... (1)Horse (2)Pegasus: 2 Horse constructor... Bird constructor... Pegasus constructor... (1)Bird (2)Pegasus: 1 Bird constructor... (1)Bird (2)Pegasus: 2 Horse constructor... Bird constructor... Pegasus constructor... Ranch[0]: Whinny!... Horse destructor... Ranch[1]: Whinny!... Pegasus destructor... Bird destructor... Horse destructor... Aviary[0]: Chirp... I can fly! I can fly! I can fly! Bird destructor... Aviary[1]: Whinny!... I can fly! I can fly! I can fly! Pegasus destructor... Bird destructor... Horse destructor... Aviary[0]: Chirp... I can fly! I can fly! I can fly! Bird destructor... Aviary[1]: Whinny!... I can fly! I can fly! I can fly! Pegasus destructor.. Bird destructor... Horse destructor...
Analysis: On lines 6-14, a Horse class is declared. The constructor and destructor print out a message, and the Whinny() method prints the word Whinny!
On lines 16-25, a Bird class is declared. In addition to its constructor and destructor, this class has two methods: Chirp() and Fly(), both of which print identifying messages. In a real program these might, for example, activate the speaker or generate animated images.
Finally, on lines 30-36, the class Pegasus is declared. It derives both from Horse and from Bird. The Pegasus class overrides the Chirp() method to call the Whinny() method, which it inherits from Horse.
Two lists are created, a Ranch with pointers to Horse on line 41, and an Aviary with pointers to Bird on line 42. On lines 46-55, Horse and Pegasus objects are added to the Ranch. On lines 56-65, Bird and Pegasus objects are added to the Aviary.
Invocations of the virtual methods on both the Bird pointers and the Horse pointers do the right things for Pegasus objects. For example, on line 78 the members of the Aviary array are used to call Chirp() on the objects to which they point. The Bird class declares this to be a virtual method, so the right function is called for each object.
Note that each time a Pegasus object is created, the output reflects that both the Bird part and the Horse part of the Pegasus object is also created. When a Pegasus object is destroyed, the Bird and Horse parts are destroyed as well, thanks to the destructors being made virtual.
Archived Comments
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Calculating total based on the given quantity and price in C++
Sorting an array of Strings in C++
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++
Compiling and Linking Multiple Source Files in C++
Escape Sequences for Nonprintable Characters in C++
Using the Built-in Arithmetic Types in C++