write a simple program for multiple inheritance in C++



The code for this program is as follows:

#include<iostream.h>
#include<conio.h>
class Parent1
{
public:
void display1()
{
cout <<"Hello from Parent1\n";
}

};

class Parent2
{
public:
void display2()
{
cout <<"Hello from Parent2\n";
}

};
class Child:public Parent1,public Parent2
{
public:
void display3()
{
display1();
display2();
cout <<"Hello from Child";
}

};
void main()
{
clrscr();
Child c;
c.display3();
getch();
}

In the above example, we have three classes Parent1, Parent2, Child.
The Child class inherits the class Parent1 and Parent2 publically as shown below:
class Child : public Parent1 , public Parent2
In this example, class Child can access the properties and methods of both class Parent1 and class Parent2.
In the class Parent1, we have defined a method display1() that will print a message when it will call.
In the class Parent2, we have defined a method display2() that will print a message when it will call.
In the class Child, we have defined a method display3(). In this method, we have called the methods display1() and display2(). When display3() method will call then display1() and display2() method will be executed.
After that the next statement of method display3() will be executed. In the main() method, we have created the object of class Child as shown below:
Child c;
Now we are calling the display3() method of class Child using object as shown below:
c.display3();
After execution of method, the output is shown as below:

Output

c++ program