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

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

Example

class A

{

// code of class A

}

class B

{

// code of class B

}

class C: public A, public B (access modifier class_name)

{

// code of the derived class

}

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

Program1.cpp

Example

#include <iostream>

using namespace std;

// create a base class1

class Base_class

{

	// access specifier

	public: 

	// It is a member function

	void display()

	{

		cout << " It is the first function of the Base class " << endl;

	}

};

// create a base class2

class Base_class2

{

	// access specifier

	public: 

	// It is a member function

	void display2()

	{

		cout << " It is the second function of the Base class " << endl;

	}

};

/* create a child_class to inherit features of Base_class and Base_class2 with access specifier. */

class child_class: public Base_class, public Base_class2

{

	

	// access specifier

	public:

	void display3()	// It is a member function of derive class

	{

		cout << " It is the function of the derived class " << endl;	

	}

	

};

int main ()

{

	// create an object for derived class

	child_class ch;

	ch.display(); // call member function of Base_class1

	ch.display2(); // call member function of Base_class2

	ch.display3(); // call member function of child_class

}

Output

Output

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 childclass invoke member function display and display2 from both parent classes Baseclass 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.

Program2.cpp

Example

#include <iostream>

using namespace std;

// create add class

class add

{

	public:

		int x = 20;

		int y = 30;

		void sum()

		{

			cout << " The sum of " << x << " and " << y << " is " <<x+y << endl;

		}

};

// create Mul class

class Mul

{

	public:

		int a = 20;

		int b = 30;

		void mul()

		{

			cout << " The Multiplication of " << a << " and " << b << " is " <<a*b << endl;

		}

};

// create Subclass

class Sub

{

	public:

		int a = 50;

		int b = 30;

		void sub()

		{

			cout << " The Subtraction of " << a << " and " << b << " is " <<a-b << endl;

		}

};

// create Div class

class Div

{

	// access specifier

	public:

		int a = 150;

		int b = 30;

		void div()

		{

			cout << " The Division of " << a << " and " << b << " is " <<a/b << endl;

		}

};

// create a derived class to derive the member functions of all base classes

class derived: public add, public Div, public Sub, public Mul

{

	// access specifier

	public:

		int p = 12;

		int q = 5;

		void mod()

		{			

			cout << "The Modulus of " << p << " and " <<q << " is " << p % q << endl;

		}

};

int main ()

{

	// create an object of the derived class

	derived dr;

	dr.mod(); // call derive class member function	

	// call all member function of class add, Div, Sub and Mul

	dr.sum();

	dr.mul();

	dr.div();

	dr.sub();

}

Output

Output

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.

Program3.cpp

Example

#include <iostream>

using namespace std;

// create base class1

class student_detail 

{

	// access specifier 

	protected:

		int rno, sum = 0, i, marks[5];

	

	// access specifier	

	public:

		void detail()

		{

			

			

			cout << " Enter the Roll No: " << endl;

			cin >> rno;

			cout << " Enter the marks of five subjects " << endl;

			// use for loop

			for (i = 0; i < 5; i++)

			{

				cin >> marks[i];

			}

			

			

			for ( i = 0; i < 5; i++)

			{

				// store the sum of five subject

			sum = sum + marks[i];

			}

		}

			

};

// create base class2

class sports_mark 

{

	protected:

		int s_mark; 

	

	public:

		

		void get_mark()

		{

			cout << "\n Enter the sports mark: ";

			cin >> s_mark;

			}	

};

/* create a result as the child class to inherit functions of the parent class: student_detail and sports_mark.*/

class result: public student_detail, public sports_mark

{

	int tot, avg;

	public:

		

		// create member function of child class

		void disp ()

		{

			tot = sum + s_mark;

			avg = tot / 6; // total marks of six subject / 6

			cout << " \n \n \t Roll No: " << rno << " \n \t Total: " << tot << endl;

			cout << " \n \t Average Marks: " << avg;

		}

};

int main ()

{

	result obj; // create an object of the derived class

	// call the member function of the base class

	obj.detail();

	obj.get_mark();

	obj.disp();

}

Output

Output

Enter the Roll No:

25

 Enter the marks of five subjects

90

85

98

80

75

 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.

Program4.cpp

Example

#include <iostream>

#include <conio.h>

using namespace std;

// create class A

class A

{

	public: 

	void show()

	{

		cout << " It is the member function of class A " << endl;

	}

 }; 

 

 // create class B

 class B

{

	public: 

	void show()

	{

		cout << " It is the member function of class B " << endl;

	}

 };

 

 

 // create a child class to inherit the member function of class A and class B

 class child: public A, public B

 {

 	public:

 		void disp()

 		{

 			cout << " It is the member function of the child class " << endl;

		 }

 };

 

 int main ()

 {

 	// create an object of the child class to access the member function

 	child ch;

 	ch.show(); // It causes ambiguity 

ch.disp();

 	return 0;

 }

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

Example

Derived_obj_name.parent_class_name : : same_named_memberFunction ( [parameter] );

For example,

Example

ch.A:: show(); // class_name and scope resolution operator with member function

ch.B::show();

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

Example

It is the member function of the child class

 It is the member function of class A

 It is the member function of class B

Input Required

This code uses input(). Please provide values below: