Multiple Inheritance in C++ with examples

It is a process or mechanism in which a child class is inherited from two parent classes. Some programming languages allows multiple inheritance like C++ and some does not allows it like Java. When a child class is inherited from two or more than two parent classes, it inherits all the properties and functionality combined of his parent classes.

Syntax of multiple inheritance:-

The syntax of multiple inheritance is as follows:

Class child_class : specifier parent_class1, spec parent_class2….

{

Body of the class

}

Example of Multiple Inheritance:-

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

class Parent1
{
protected:
int a;

public:

Parent1()
{
a = 1;
}
};

class Parent2
{

protected:
int b;

public:

Parent2()
{
b= 2;
}

};

class Child: public Parent1, public Parent2
{
private:
int c;

public:

Child()
{
c= 0;
c= a + b;
}

void show()
{
std::cout<<"The value of data members of both classes A and B is "<<c<< std::endl;
}
};

main()
{
Child obj;
obj.show();
getch();
}

Multiple inheritance example 1Explanation:-

In above example, we have created three classes, we named two parent classes as Parent1 and parent2. We have another class named as Child class which is inherited from both parent classes. This class contain protected data members present on both parent classes. Now in a main function, when we creates an object of child class, its constructor will run and it will add two variables ‘a’ and ‘b’ and store them into variable ‘c’. When we call its function “Show”, it will print the values on a computer screen.

Constructors in multiple inheritance:-

When we creates an object of a child class, then the constructors of parent classes automatically executes. In multiple inheritance constructors could parameterized and they could be non-parameterized.

For example:-

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

class Parent1
{
private:

int a;

public:

Parent1()
{
a=0;
}

Parent1(int n)
{
a=n;
}

void ShowParent1 ()
{
std::cout<<"a= "<<a<<std::endl;
}
};

class Parent2
{
private:

int b;

public:

Parent2()
{
b=0;
}
Parent2(int n)
{
b=n;
}

void ShowParent2 ()
{
std::cout<<"b= "<<b<<std::endl;
}
};

class Child: public Parent1, public Parent2
{
private:

int c;

public:

Child(): Parent2(), Parent1()
{
c=0;
}

Child(int x, int y, int z) : Parent1(x), Parent2(y)
{
c=z;
}

void ShowChild()
{
Parent1::ShowParent1();
Parent2::ShowParent2();
std::cout<<"c= "<<c<<std::endl;
}
};

main()
{
Child obj(1,2,3);
obj.ShowChild();
getch();
}

Multiple inheritance example 2Explanation:-

We have created three classes Parent1, Parent2 and a Child class. All of these classes have parameterized constructors, we could access constructor of parent class by using a scope resolution operator (‘:’). In a main function we will create an parameterized child object and we will call its showChild() function.

How to remove ambiguity in Multiple inheritance:-

In multiple inheritance the most common problem we encounter is ambiguity. It occurs when two or more parent classes have function of same names, which confuses the compiler about which function to execute. Therefore in order to deal with an ambiguity we use class name + scope resolution operator + function name, which tells compiler, which class function we want to execute.

For example:-

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

class Parent1
{

public:

void show()
{
std::cout << "Parent1 Constructor is running......." << std::endl;
}
};

class Parent2
{
public:

void show()
{
std::cout << "Parent2 Constructor is running......." << std::endl;
}
};

class Child : public Parent1, public Parent2
{
public:

void show()
{
Parent1::show();
Parent2::show();
}
};

main()
{
Child obj;
obj.show();
getch();
}

Multiple inheritance example 3Explanation:-

In above example we have inherited a child class from two parent classes, I a function “show()” of a child class we have removed ambiguity by using a scope resolution operator along with a class name.

3/5 - (2 votes)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a Reply

HellGeeks
Logo