Multiple inheritance in C++ is a feature that allows a class to inherit from more than one base class, which means a derived class can have multiple parent classes and inherit attributes and behaviors from all the base classes.
Implementing Multiple Inheritance
To implement multiple inheritance, you need to specify multiple base classes in the derived class and declare it using a comma-separated list.
Syntax
The syntax of multiple inheritance in C++ is −
classBase1{// Base class 1 members};classBase2{// Base class 2 members};classDerived:public Base1, public Base2{// Derived class members};
Block Diagram of Multiple Inheritance
See the below block diagram demonstrating multiple inheritance −

As per the above diagram, classes “Car” and “Boat” are the base classes and they are deriving over “DualModeVehicle” class in order to implement multiple inheritance.
Example of Multiple Inheritance
In the following example, there are two base classes, “Car” and “Boat”, and one derived class, which is “DualModeVehicle”. Both of the base classes are inherited by the derived class.
#include <iostream>usingnamespace std;// Base class 1classCar{public:voiddrive(){
cout <<"Driving on land"<< endl;}};// Base class 2classBoat{public:voidsail(){
cout <<"Sailing on water"<< endl;}};// Derived classclassDualModeVehicle:public Car, public Boat{public:voiduse(){drive();// Calls the drive function from Carsail();// Calls the sail function from Boat}};intmain(){
DualModeVehicle myVehicle;
myVehicle.use();// Demonstrates both functionalitiesreturn0;}
Output
Driving on land Sailing on water
Explanation
- Class Car is our first base class with public member function drive(), whereas class boat is the second base class with public member function sail().
- Now a derived class named DualModeVehicle, which inherits from both Car and Boat and uses multiple inheritance to combine the functionalities of both base classes.
- Where it has a public member function use(), which calls the drive() method from the Car class and the sail() method from the Boat class.
Main function
- Now here, an instance of DualModeVehicle, named myVehicle, is created.
- where the use() method of myVehicle is called, which in turn calls both drive() and sail().
- returns 0 indicates successful execution.
Challenges in Multiple Inheritance
Multiple inheritance in C++ allows a class to inherit from more than one base class which provides flexibility and reusability. However, it also introduces several challenges, discussed below −
- Ambiguity − When two or more base classes have members with the same name cause ambiguity.
- Diamond problem − It arises when a class inherits from two classes that both inherit from a common base class which causes ambiguity and conflicts due to multiple copies of the base class, which is ultimately known as the Diamond problem.
Ambiguity in Multiple Inheritance
If two or more base classes have members (functions or variables) with the same name, the compiler won’t be able to decide which one to use, which ultimately leads to ambiguity.
This can be resolved using scope resolution.
Syntax
classBase1{public:voidshow(){ cout <<"Base1 show"<< endl;}};classBase2{public:voidshow(){ cout <<"Base2 show"<< endl;}};classDerived:public Base1, public Base2{public:voidshow(){Base1::show();// Explicitly calls Base1's showBase2::show();// Explicitly calls Base2's show}};
Handling Ambiguity in Multiple Inheritance
Here we will demonstrate how to handle ambiguity in multiple inheritance by using explicit scope resolution to specify which base class’s method should be called.
Example
Lets consider it with an example
#include <iostream>usingnamespace std;classBase1{public:voidshow(){
cout <<"Base1 show"<< endl;}};classBase2{public:voidshow(){
cout <<"Base2 show"<< endl;}};classDerived:public Base1, public Base2{public:voidshow(){// Ambiguity occurs here because both Base1 and Base2 have a show() methodBase1::show();// Explicitly calls Base1's showBase2::show();// Explicitly calls Base2's show}};intmain(){
Derived obj;
obj.show();// Calls Derived show() method, which resolves ambiguityreturn0;}
Output
Base1 show Base2 show
In int main() body, we had called Deriveds show() method, which resolved the ambiguity.
Diamond Problem in Multiple Inheritance
A diamond problem in C++ occurs when a class inherits from two classes that both inherit from a common base class, which ultimately creates ambiguity in the inheritance hierarchy as the derived class now has two copies of the common base class, leading to conflicts.
Example
#include <iostream>usingnamespace std;classBase{public:voidshow(){
cout <<"Base show"<< endl;}};classDerived1:public Base{};classDerived2:public Base{};classFinal:public Derived1, public Derived2{};intmain(){
Final obj;// obj.show(); // This line will cause ambiguityreturn0;}
Diamond Problem Solution in Multiple Inheritance
The primary solution for the Diamond Problem in C++ is to use virtual inheritance.
Example
#include <iostream>usingnamespace std;classBase{public:voidshow(){
cout <<"Base show"<< endl;}};classDerived1:virtual public Base{};// Virtual inheritanceclassDerived2:virtual public Base{};// Virtual inheritanceclassFinal:public Derived1, public Derived2{};intmain(){
Final obj;
obj.show();// Now this calls Base's show() without ambiguityreturn0;}
Output
Base show
By using virtual inheritance, we can avoid the Diamond problem challenge, which ensures that only one instance of the base class exists in the derived class hierarchy.
Benefits of Using Multiple Inheritance
- Code reusability, as it allows developers to use existing classes to create new classes with combined functionalities.
- It models real-world entities more accurately, where a derived class may have characteristics of multiple base classes.
- It enables a more flexible and modular design.
Leave a Reply