The property of acquiring all the properties and behaviors of the parent object by an object is termed as inheritance in OOPs. This is a unique feature in object oriented programming languages which facilitates reusability of the code of the parent class by the derived class.
CPP Multilevel Inheritance:
CPP facilitates inheritance of a derived class from its base class as well as inheritance of a derived class from another derived class. The inheritance of a derived class from another derived class is called as multilevel inheritance in CPP, which is possible to any level.
Example 1: Example of single level inheritance
#include <iostream.h> using namespace std; class Employee { public: string name = "Ved"; }; class Details: public Employee { public: float salary = 50000; }; int main() { Details e1; cout<<"Name: "<<e1.name<<endl; cout<<"Salary: "<<e1.salary<<endl; return 0; } |
Output
Name: Ved Salary: 50000 |
Example 2: Example of multilevel inheritance
#include <iostream.h> using namespace std; class Employee { public: string name = "Ved"; }; class Salary: public Employee { public: float salary = 50000; }; class Experience: public Salary { public: string experience = "5 years"; }; int main() { Experience e1; cout<<"Name: "<<e1.name<<endl; cout<<"Salary: "<<e1.salary<<endl; cout<<"Experience: "<<e1.experience<<endl; return 0; } |
Output
Name: Ved Salary: 50000 Experience: 5 years |