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 InheritanceTo implement multiple inheritance, you need to specify multiple base classes in the derived class and declare it using a comma-separated list.
SyntaxThe syntax of multiple inheritance in C++ is −
class Base1 { // Base class 1 members }; class Base2 { // Base class 2 members }; class Derived : 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 InheritanceIn 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> using namespace std; // Base class 1 class Car { public: void drive() { cout << "Driving on land" << endl; } }; // Base class 2 class Boat { public: void sail() { cout << "Sailing on water" << endl; } }; // Derived class class DualModeVehicle: public Car, public Boat { public: void use() { drive(); // Calls the drive function from Car sail(); // Calls the sail function from Boat } }; int main() { DualModeVehicle myVehicle; myVehicle.use(); // Demonstrates both functionalities return 0; }Output
Driving on land Sailing on waterExplanation
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 −
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.
Syntaxclass Base1 { public: void show() { cout << "Base1 show" << endl; } }; class Base2 { public: void show() { cout << "Base2 show" << endl; } }; class Derived : public Base1, public Base2 { public: void show() { Base1::show(); // Explicitly calls Base1's show Base2::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.
ExampleLets consider it with an example
#include <iostream> using namespace std; class Base1 { public: void show() { cout << "Base1 show" << endl; } }; class Base2 { public: void show() { cout << "Base2 show" << endl; } }; class Derived : public Base1, public Base2 { public: void show() { // Ambiguity occurs here because both Base1 and Base2 have a show() method Base1::show(); // Explicitly calls Base1's show Base2::show(); // Explicitly calls Base2's show } }; int main() { Derived obj; obj.show(); // Calls Derived show() method, which resolves ambiguity return 0; }Output
Base1 show Base2 show
In int main() body, we had called Deriveds show() method, which resolved the ambiguity.
Diamond Problem in Multiple InheritanceA 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> using namespace std; class Base { public: void show() { cout << "Base show" << endl; } }; class Derived1 : public Base {}; class Derived2 : public Base {}; class Final : public Derived1, public Derived2 {}; int main() { Final obj; // obj.show(); // This line will cause ambiguity return 0; }Diamond Problem Solution in Multiple Inheritance
The primary solution for the Diamond Problem in C++ is to use virtual inheritance.
Example#include <iostream> using namespace std; class Base { public: void show() { cout << "Base show" << endl; } }; class Derived1 : virtual public Base {}; // Virtual inheritance class Derived2 : virtual public Base {}; // Virtual inheritance class Final : public Derived1, public Derived2 {}; int main() { Final obj; obj.show(); // Now this calls Base's show() without ambiguity return 0; }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 InheritanceRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4