This segment will cover the concept of Multiple Inheritances within the C++ programming language. When we inherit the characteristics and capabilities of one class into another class, it is known as Inheritance. Through this mechanism, we are able to recycle, expand, or adjust all the properties and actions of the base class by utilizing the derived class object. Inheritance stands as a crucial element of object-oriented programming that effectively minimizes the code length.
A derived class inherits all methods and capabilities from another class known as the parent class. The base or parent class is where the derived class obtains certain characteristics.
Multiple Inheritance refers to the principle in C++ where a derived class can inherit attributes or actions from multiple base classes. This feature allows a subclass to access methods, attributes, and traits from multiple parent classes simultaneously.
Diagram of the Multiple Inheritance
Below is the illustration of Multiple Inheritance in the C++ coding language.
In the diagram above, we observe the presence of two parent classes: Base Class 1 and Base Class 2, with a single Child Class inheriting from both. This results in the Child Class inheriting all properties and methods from both Base Class 1 and Base Class 2. Hence, this type of inheritance is referred to as Multiple Inheritance.
Syntax of the Multiple Inheritance
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 provided syntax, class A and class B serve as two parent classes, while class C acts as the derived class inheriting characteristics from its parent classes.
Let's explore different examples of Multiple Inheritance in programming, where we can inherit member functions and features from multiple base classes through the derived class.
Example 1: Program to use the Multiple Inheritance
Program1.cpp
#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
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 preceding code, we established a pair of base classes along with a derived class. Through the childclass object ch, the child class calls the display and display2 member functions from both the parent classes Baseclass and Base_class2.
Example 2: Use Multiple Inheritance to perform the arithmetic operation
Let's establish a derived class to inherit the member functions from several base classes in C++ development.
Program2.cpp
#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
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 develop a new application to display the mean scores of six subjects by leveraging multiple Inheritance within the C++ programming language.
Program3.cpp
#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
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 the concept of Multiple Inheritance, a scenario arises where a derived class inherits from two or more base or parent classes. Consequently, there is a possibility that these parent classes contain member functions with identical names, leading to ambiguity when the child class object calls one of these same-named member functions. As a result, the C++ compiler faces difficulty in determining which class's member function to execute within the program.
Program to demonstrate the Ambiguity Problem in Multiple Inheritance
Let's create a straightforward example in C++ where we call the identical member function of the base class using the derived class.
Program4.cpp
#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 compiling the aforementioned program, an error occurs due to ambiguity in the show member function. This ambiguity arises from both the base classes A and B defining the show function. Consequently, when the object of the derived class calls the show function, it encounters ambiguity in multiple inheritances.
Therefore, it is necessary to address the ambiguous issue that arises in the context of multiple inheritance. This ambiguity can be effectively resolved by explicitly stating the class name and utilizing the scope resolution (::) operator to indicate the specific class from which the member function should be called within the child class.
Syntax of the Ambiguity Resolution
Derived_obj_name.parent_class_name : : same_named_memberFunction ( [parameter] );
For example,
ch.A:: show(); // class_name and scope resolution operator with member function
ch.B::show();
After implementing modifications, we once again run the aforementioned program which produces the following Output displayed below.
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