This tutorial introduces C++'s multiple inheritance capabilities. The tutorial focuses on multiple inheritance syntax. It also demonstrates how to access inherited members of a derived class when those members have the same name in each of the base classes. This tutorial is intended for students and developers who are already familiar with single inheritance in C++. In our subsequent tutorial, Multiple Inheritance and virtual Base Classes, we continue our presentation of multiple inheritance by investigating the so-called diamond inheritance problem.
Download the code examples for this tutorial.
[Note: This tutorial is an excerpt (Section 24.7) of Chapter 24, Other Topics, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.1213-1218. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
24.7 Multiple Inheritance (Continued)
Class Derived (Fig. 24.9–Fig. 24.10) inherits from both class Base1 and class Base2 through multiple inheritance. Class Derived has a private data member of type double named real (line 21), a constructor to initialize all the data of class Derived and a public member function getReal that returns the value of double variable real.
1 // Fig. 24.9: Derived.h 2 // Definition of class Derived which inherits 3 // multiple base classes (Base1 and Base2). 4 #ifndef DERIVED_H 5 #define DERIVED_H 6 7 #include <iostream> 8 using std::ostream; 9 10 #include "Base1.h" 11 #include "Base2.h" 12 13 // class Derived definition 14 class Derived : public Base1, public Base2 15 { 16 friend ostream &operator<<( ostream &, const Derived & ); 17 public: 18 Derived( int, char, double ); 19 double getReal() const; 20 private: 21 double real; // derived class's private data 22 }; // end class Derived 23 24 #endif // DERIVED_H |
Derived.h.
1 // Fig. 24.10: Derived.cpp 2 // Member function definitions for class Derived 3 #include "Derived.h" 4 5 // constructor for Derived calls constructors for 6 // class Base1 and class Base2. 7 // use member initializers to call base-class constructors 8 Derived::Derived( int integer, char character, double double1 ) 9 : Base1( integer ), Base2( character ), real( double1 ) { } 10 11 // return real 12 double Derived::getReal() const 13 { 14 return real; 15 } // end function getReal 16 17 // display all data members of Derived 18 ostream &operator<<( ostream &output, const Derived &derived ) 19 { 20 output << " Integer: " << derived.value << "\n Character: " 21 << derived.letter << "\nReal number: " << derived.real; 22 return output; // enables cascaded calls 23 } // end operator<< |
Derived.cpp.
Notice how straightforward it is to indicate multiple inheritance by following the colon (:) after class Derived with a comma-separated list of base classes (line 14). In Fig. 24.10, notice that constructor Derived explicitly calls base-class constructors for each of its base classes—Base1 and Base2—using the member-initializer syntax (line 9). The base-class constructors are called in the order that the inheritance is specified, not in the order in which their constructors are mentioned; also, if the base-class constructors are not explicitly called in the member-initializer list, their default constructors will be called implicitly.
Other tutorials on this topic:
24.7 Multiple Inheritance (You are here).
24.8 Multiple Inheritance and virtual Base Classes

