Multiple Inheritance in C++ Programming

Inheritance is the process of inheriting properties of objects of one class by objects of another class. The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class. When a class is derived from two or more base classes, such inheritance is called Multiple Inheritance. It allow us to combine the features of several existing classes into a single class.

For example,

  • Petrol is derived from both liquid and fuel.
  • A child has character of both his/her father and mother, etc

Syntax of Multiple Inheritance

class base_class1
{
    properties;
    methods;
};

class base_class2
{
    properties;
    methods;
};
... ... ...
... ... ...
class base_classN
{
    properties;
    methods;
};

class derived_classname : visibility_mode base_class1, visibility_mode base_class2,... ,visibility_mode base_classN
{
    properties;
    methods;
};

multiple inheritance in c++

Ambiguity in Multiple Inheritance

In multiple inheritance, a single class is derived from two or more parent classes. So, there may be a possibility that two or more parents have same named member function. If the object of child class needs to access one of the same named member function then it results in ambiguity. The compiler is confused as method of which class to call on executing the call statement.

For example,

#include <iostream>
#include <conio.h>

using namespace std;

class A
{
  public:
    void display()
    {
        cout <<"This is method of A";
    }
};

class B
{
  public:
    void display()
    {
        cout <<"This is method of B";
    }
};

class C: public A, public B

{
  public:
};

int main()
{
    C sample;
    sample.display();  /*causes ambiguity*/
    getch();
    return 0;
}

Ambuity Resolution of Multiple Inheritance in C++

This problem can be resolved by class name and using scope resolution operator to specify the class whose method is called.


Syntax

derived_objectname.parent_classname::same_named_function([parameter]);

In the above example, if we want to call the method of class A then we can call it as below,

sample.A::display();

Similarly, if we need to call the method of class B then,

sample.B::display();

Example of Multiple Inheritance in C++

C++ program to display petrol's data using Multiple Inheritance from fuel and liquid

#include <iostream>
#include <conio.h>
using namespace std;

class liquid
{
    float specific_gravity;
    public:
        void input()
        {
            cout<<"Specific gravity: ";
            cin>>specific_gravity;
        }
        void output()
        {
            cout<<"Specific gravity: "<<specific_gravity<<endl;
        }
};

class fuel
{
    float rate;
    public:
        void input()
        {
            cout<<"Rate(per liter): $";
            cin>>rate;
        }
        void output()
        {
            cout<<"Rate(per liter): $"<<rate<<endl;
        }
};

class petrol: public liquid, public fuel
{
    public:
        void input()
        {
            liquid::input();
            fuel::input();
        }
        void output()
        {
            liquid::output();
            fuel::output();
        }
};

int main()
{
    petrol p;
    cout<<"Enter data"<<endl;
    p.input();
    cout<<endl<<"Displaying data"<<endl;
    p.output();
    getch();
    return 0;
}

Output

Enter data
Specific gravity: 0.7
Rate(per liter): $0.99

Displaying data
Specific gravity: 0.7
Rate(per liter): $0.99

In this program, petrol is derived from fuel having attribute rate and liquid having attribute specific gravity. So the public features of both fuel and petrol are inherited to petrol. Every class has a method named input() for providing input and another method named output() to display the data.