Multiple Inheritance in C++



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 −

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 −

C++ 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>
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 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

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() {
         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.

Example

Lets 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 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>
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 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.
Advertisements