Multilevel Inheritance in C++ Programming
Inheritance is a basic object oriented feature in which one class acquires and inherit the properties of another class. All the properties of the Base Class ( also known as the Parent Class or Super class ) are present in the Derived Class ( also known as the Child Class or Sub class ). When a class inherit a class, which inherit another class then this is called multilevel inheritance.
Example :
class Son inherit class Father and class Father inherit class Grandfather then this type of inheritance is known as multilevel inheritance.
In this program, there are three classes grandfather, father, and son.
- The grandfather class has a public member function house() that prints the message "3BHK House."
- The father class is derived from the grandfather class using the public keyword. This indicates that the public members of the grandfather class are accessible from the father class. The father class has a public member function land() that prints the message "5Arcs of Land."
- The son class is derived from the father class using the public keyword. This indicates that the public members of the father class are accessible from the son class. The son class has a public member function car() that prints the message "Audi Car."
- In the main() function, an object o of the son class is created. Since the son class is derived from the father class, it can access the land() member function of the father class using the dot (.) operator. Similarly, since the father class is derived from the grandfather class, it can access the house() member function of the grandfather class using the same dot (.) operator. Finally, the car() member function of the son class is called using the same object o.
- The son class has inherited the land() member function from the father class, which in turn has inherited the house() member function from the grandfather class. The son class has defined its own member function car(). The main() function calls all three functions using the object of the son class.
Source Code
#include<iostream>
using namespace std;
//Multilevel Inheritance in C++ Programming
class grandfather
{
public:
void house()
{
cout<<"3BHK House."<<endl;
}
};
class father:public grandfather
{
public:
void land()
{
cout<<"5Arcs of Land."<<endl;
}
};
class son:public father
{
public:
void car()
{
cout<<"Audi Car."<<endl;
}
};
int main()
{
son o;
o.house();
o.land();
o.car();
return 0;
}
Output
3BHK House.
5Arcs of Land.
Audi Car.
To download raw file
Click Here