write a simple program for multilevel inheritance in C++



The code for this program is as follows:

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

}

};

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

class Three:public Two
{
public:
void display3()
{
display1();
display2();
cout <<"Hello from three";

}
};

void main()
{
clrscr();
Three t;
t.display3();
getch();

}

In the above example, we have used multilevel inheritance, and created three class One, Two, Three. In this example, class Two inherits the class One and class Three inherits the class Two. The methods defined in the class One are accessible to both class Two and Three. In this example, class Three inherits the class Two and class Two inherits the class One, thus class Two can access the properties of class One only but class Three can access the properties of both class One and class Two because class Two inherits the class One and class Two has the properties of class One.
In the class One, we have defined a method display1() that will print a message Hello from One when it will call.
In the class Two, we have defined a method display2() that will print a message Hello from Two when it will call.
In the class Three, we have defined a method display3(). In this method, we called the method display1(), display2() directly. When display3() method will call then first display1() and display2() methods will execute and after completion of these methods, next statement of display3() method will execute.
In the main() method, we have created the object of class Three as shown below:
Three t;
Now we are calling the method of class Three as following:
t.display3();
Below is the output of the program:

Output

Hello from One
Hello from Two
Hello from Three