C++

C++ Multiple inheritance

Inheritance is the process of enhancing and extending the existing classes without causing any change in them. This causes a hierarchical relation between the classes. Inheritance in the C++ programming language is derived into 5 main types. Which are:

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance

Here our topic of discussion is multiple inheritances.

Multiple inheritances

Multiple inheritance occurs when more than one class operates as a parent class. In the C++ programming language, we can derive a class from the base class, but also we can derive a class from those classes that are also derived from another base class. We call this sort of inheritance multiple inheritances. During this process, some issues also occur; these are explained later in the guide.

Purpose of inheritance

Inheritance is one of the useful and important approaches of OOP (object-oriented programming). The major purpose of using inheritance in OOP or c++ is to enable the user to re-use the source code. Instead of writing the same code several times, we can simply use inheritance to inherit the properties of a base class to the child class. This also reduces the complexity of the source code.

The syntax for multiple inheritances are:

class 1 {
};
class 2: public 1 {
};
class 3: public 2 {
};

To inherit any class, the name of the child class is written on the left side, whereas the parent class is written on the right side, and this is separated by a colon. In the above syntax, class 2 is derived from class 1, and class 3 is derived from class 2.

Implementation of multiple inheritance

Example 1

This example is a sample use of inheritance as multiple inheritances contain a child with two parents. So here, class C is a child class, whereas class A and class B are parent classes. Each class includes a constructor that will display a message whenever the class object is created. Using the library iostream helps in declaring cin, cout functionalities. Both the parent classes are declared in any manner, but while declaring the child class, we need to mention both parent classes inherited from the child class. Here the order of both the classes must be kept in mind. The class that is declared first must be mentioned last.

For instance, in the given example, class A is created first in the source code to be mentioned after B, and class B will be mentioned first.

Class C: public B, public A

We will create an object for only the child class in the main program, as in the inheritance process, the child class object automatically accesses the parent class. Now save the file with the ‘.c’ extension, compile the code through a G++ compiler. ‘-o’ is utilized to save the file’s output.

$ g++ -o multi-multi.c
$ ./multi

On successful execution, the answer will be displayed. You can see that class B’s constructor will be executed first just because of the required order, although it was declared later, then the constructor of class A is executed, and at the end, the child class itself gets executed.

Example 2

Here is an example of multiple inheritances where a child-class ‘doll’ contains two parents, a toy, and a plushtoy. These classes are declared the same way as we did in the last example. Each constructor is created for both the parent classes. The third class, the child class, is only declared, but its constructor is not created; there is only a declaration. Here the order for the declaration of parent class is changed, as the class toy is declared first, must be mentioned later, but at the time of declaring child class, it is mentioned first; similarly, class plushtoy is declared later also mentioned after the class toy.

While creating the object, the parent class constructors will be executed depending on the order mentioned while declaring the child class.

Now execute the code; you will see that the constructor of the parent toy is executed first, and then the constructor of the class “plushtoy” is executed.

Example 3

A diamond problem occurs in the situation when two parents (superclass) have a common base class. For example, child C has two parents, A and B, and these both belong to a single base class D. So, child class C will indirectly contain two copies of class D, which leads to ambiguity. This problem is explained via C++ source code.

A class person is created as a base class and has a constructor to display a message. Another class Faculty is created that is a child of the base class ‘person’ and has a constructor in the public part of the class. Like faculty, a class student is also created that inherits from the parent base class person.

Faculty and students become superclass parents in both classes when a class TA is derived from them. This child class TA is declared by having the names of both parent classes.

Class TA: public Faculty: public student

This forms a diamond; two classes inherit a single class, whereas a single class is inherited from these two classes. So indirectly, the child class will contain data from the base class of its parents.

The object creation will initiate all the constructors of the classes described above.

When the program runs, you will see that the faculty class constructor will be executed first as the order was that it was mentioned first while declaring a child class. So the faculty class will first display the person class message and then its message; similarly, the student class will again print the person class first and then its message. And at the end, the child class message is displayed.

If any process or operation is declared in the base class, it will be copied twice. This will cause ambiguity.

To remove this issue, there is a solution. The keyword ‘virtual’ is used in this aspect. Using the virtual keyword with the parent class, faculty, and the student will avoid the creation of two copies of a base class. This is applied to an example displayed below.

The remaining code is the same. This will work so that the base class constructor (person) is called by default when the object is created. This will eliminate duplication of the same message or any operation.

Now we will execute the code to see the results. The base class constructor is executed only a single time.

Conclusion

‘Multiple inheritance C++’ contains the basic explanation of inheritance and includes it in the code. Some elementary examples are implemented in C++ to explain the working of multiple inheritances. This guide comes with some major inheritance issues like the diamond problem. It is elaborated in detail, and also the solution to it is also highlighted. Hopefully, this effort will be a source of complete knowledge for the new users.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.