Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

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

In Chapter 9 and Chapter 10, we discussed single inheritance, in which each class is derived from exactly one base class. In C++, a class may be derived from more than one base class—a technique known as multiple inheritance in which a derived class inherits the members of two or more base classes. This powerful capability encourages interesting forms of software reuse but can cause a variety of ambiguity problems. Multiple inheritance is a difficult concept that should be used only by experienced programmers. In fact, some of the problems associated with multiple inheritance are so subtle that newer programming languages, such as Java and C#, do not enable a class to derive from more than one base class.

Good Programming Practice
Good Programming Practice 24.2
Multiple inheritance is a powerful capability when used properly. Multiple inheritance should be used when an “is a” relationship exists between a new type and two or more existing types (i.e., type A “is a” type B and type A “is a” type C).
Software Engineering Observation
Software Engineering Observation 24.4
Multiple inheritance can introduce complexity into a system. Great care is required in the design of a system to use multiple inheritance properly; it should not be used when single inheritance and/or composition will do the job.

A common problem with multiple inheritance is that each of the base classes might contain data members or member functions that have the same name. This can lead to ambiguity problems when you attempt to compile. Consider the multiple-inheritance example (Fig. 24.7, Fig. 24.8, Fig. 24.9, Fig. 24.10, Fig. 24.11). Class Base1 (Fig. 24.7) contains one protected int data member—value (line 20), a constructor (lines 10–13) that sets value and public member function getData (lines 15–18) that returns value.
 1  // Fig. 24.7: Base1.h
 2  // Definition of class Base1
 3  #ifndef BASE1_H
 4  #define BASE1_H
 6  // class Base1 definition
 7  class Base1
 8  {
 9  public:
10     Base1( int parameterValue )
11     {
12        value = parameterValue;
13     } // end Base1 constructor
15     int getData() const
16     {
17        return value;
18     } // end function getData
19  protected: // accessible to derived classes
20     int value; // inherited by derived class
21  }; // end class Base1
23  #endif // BASE1_H
Fig. 24.7 Demonstrating multiple inheritance—Base1.h.

Class Base2 (Fig. 24.8) is similar to class Base1, except that its protected data is a char named letter (line 20). Like class Base1, Base2 has a public member function getData, but this function returns the value of char data member letter.

 1  // Fig. 24.8: Base2.h
 2  // Definition of class Base2
 3  #ifndef BASE2_H
 4  #define BASE2_H
 6  // class Base2 definition
 7  class Base2
 8  {
 9  public:
10     Base2( char characterData )
11     {
12        letter = characterData;
13     } // end Base2 constructor
15     char getData() const
16     {
17        return letter;
18     } // end function getData
19  protected: // accessible to derived classes
20     char letter; // inherited by derived class
21  }; // end class Base2
23  #endif // BASE2_H
Fig. 24.8 Demonstrating multiple inheritance—Base2.h.

Additional Pages in this Tutorial:
   Page 1 | 2 | 3

Other tutorials on this topic:
24.7 Multiple Inheritance (You are here).
24.8 Multiple Inheritance and virtual Base Classes

Tutorial Index