Multiple Inheritance
Multiple inheritance is a type of inheritance where classes can inherit from more than one base class.
Syntax
The syntax for multiple inheritance is similar to the syntax for single inheritance:
class DerivedClass : public BaseClass1, public BaseClass2 {
// Derived class members
};
Classes can inherit from as many base classes as necessary, and, as with single inheritance, access modifiers can be used to modify access for inherited members.
The ordering of base classes affects the order in which constructors and destructors are called:
- Virtual base class constructors are called first (from left to right).
- Direct base class constructors are then called (from left to right).
- Destructors are called in reverse order of constructors.
Examples
Basic multiple inheritance
class Bat {public:void fly();};class Man {public:void run();};class ManBat : public Man, public Bat {// run() and fly() are both available as they're inherited from Man and Bat};
Ambiguity errors
With multiple inheritance, ambiguity errors can occur if two base classes have members with identical names. This happens because it is unclear which base class member should be referenced. The scope resolution operator (::
) can resolve such conflicts.
class Dancer {public:void jump();};class Frog {public:void jump();};class DancingFrog : public Dancer, public Frog {// jump() is inherited from Dancer and Frog};int main () {DancingFrog df;df.jump(); // This will cause an ambiguity error as it isn't clear which ancestor's jump() we meandf.Dancer::jump(); // This won't cause an ambiguity error as the scope resolution operator explicitly references Dancerreturn 0;}
The Diamond Problem
Ambiguity errors can also occur if multiple base classes inherit from a common ancestor. This is known as the Diamond Problem.
class Vehicle {public:void weight();};class Car : public Vehicle {// Inherits weight() from Vehiclepublic:void drive();};class Plane : public Vehicle {// Inherits weight() from Vehiclepublic:void fly();};class CarPlane : public Car, public Plane {// Inherits drive() from Car and fly() from Plane// Inherits weight() twice, once from Car and once from Plane, as each will have their own Vehicle objectpublic:void switchMode();};int main () {CarPlane cp;cp.weight(); // Causes an ambiguity error as CarPlane as there are two Vehicle ancestor objectsreturn 0;}
Resolving the Diamond Problem with Virtual Inheritance
One way to fix the diamond problem is to use virtual inheritance in the intermediate classes.
class Vehicle {public:void weight();};class Car : virtual Vehicle {// Inherits weight() from Vehiclepublic:void drive();};class Plane : virtual Vehicle {// Inherits weight() from Vehiclepublic:void fly();};class CarPlane : public Car, public Plane {// Inherits drive() from Car and fly() from Plane// Inherits weight() once, as Car and Plane both reference the same Vehicle ancestor objectpublic:void switchMode();};int main () {CarPlane cp;cp.weight(); // This won't cause an ambiguity error as there is only one Vehicle ancestor objectreturn 0;}
Codebyte Example
Here’s an example demonstrating multiple inheritance using a workplace scenario:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours