Javatpoint Logo
Javatpoint Logo

Multiple Inheritance in C++

This section will discuss the Multiple Inheritances in the C++ programming language. When we acquire the features and functionalities of one class to another class, the process is called Inheritance. In this way, we can reuse, extend or modify all the attributes and behaviour of the parent class using the derived class object. It is the most important feature of object-oriented programming that reduces the length of the program.

A class that inherits all member functions and functionality from another or parent class is called the derived class. And the class from which derive class acquires some features is called the base or parent class.

Multiple Inheritance in C++

Multiple Inheritance is the concept of the Inheritance in C++ that allows a child class to inherit properties or behaviour from multiple base classes. Therefore, we can say it is the process that enables a derived class to acquire member functions, properties, characteristics from more than one base class.

Diagram of the Multiple Inheritance

Following is the diagram of the Multiple Inheritances in the C++ programming language.

Multiple Inheritance in C++

In the above diagram, there are two-parent classes: Base Class 1 and Base Class 2, whereas there is only one Child Class. The Child Class acquires all features from both Base class 1 and Base class 2. Therefore, we termed the type of Inheritance as Multiple Inheritance.

Syntax of the Multiple Inheritance

In the above syntax, class A and class B are two base classes, and class C is the child class that inherits some features of the parent classes.

Let's write the various program of Multiple Inheritance to inherit the member functions and functionality from more than one base class using the derived class.

Example 1: Program to use the Multiple Inheritance



It is the first function of the Base class
 It is the second function of the Base class
 It is the function of the derived class

In the above program, we created two base classes and one child class. The child_class invoke member function display() and display2() from both parent classes Base_class and Base_class2 with the help of child class's object ch.

Example 2: Use Multiple Inheritance to perform the arithmetic operation

Let's create a derived class to inherit the member functions from multiple base classes in C++ programming.



The Modulus of 12 and 5 is 2
 The sum of 20 and 30 is 50
 The Multiplication of 20 and 30 is 600
 The Division of 150 and 30 is 5
 The Subtraction of 50 and 30 is 20

Example 3: Get the average marks of six subjects using the Multiple Inheritance

Let's create another program to print the average marks of six subjects using the multiple Inheritance in the C++ programming language.



Enter the Roll No:
 Enter the marks of five subjects

 Enter the sports mark: 99

         Roll No: 25
         Total: 527

         Average Marks: 87

Ambiguity Problem in Multiple Inheritance

In Multiple Inheritance, when a single class is derived from two or more base or parent classes. So, it might be possible that both the parent class have the same-named member functions, and it shows ambiguity when the child class object invokes one of the same-named member functions. Hence, we can say, the C++ compiler is confused in selecting the member function of a class for the execution of a program.

Program to demonstrate the Ambiguity Problem in Multiple Inheritance

Let's write a simple to invoke the same member function of the parent class using derived class in C++ programming.


When the above program is compiled, it throws the show() member function is ambiguous. Because of both the base class A and B, defining the same member function show(), and when the derived class's object call the shows() function, it shows ambiguity in multiple inheritances.

Therefore, we need to resolve the ambiguous problem in multiple Inheritance. The ambiguity problem can be resolved by defining the class name and scope resolution (::) operator to specify the class from which the member function is invoked in the child class.

Syntax of the Ambiguity Resolution

For example,

After making some changes, now we again execute the above program that returns the given below Output.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA