Multilevel Inheritence When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent classes, such inheritance is called Multilevel Inheritance
Multilevel Inheritence Child class The class which inherits the properties of another class is called Derived or Child or Sub class Parent class The class whose properties are inherited is called Base or Parent or Super class.
Syntax of Multilevel Inheritance class A //Base class { --------- --------- //body of class A --------- }; class B : public A //B derived from class A { ---------- --------- //body of class B ---------- };
Syntax of Multilevel Inheritance class C : public B //C derived from class B { ---------- --------- //body of class C ---------- }; //This process can be extended to any number of levels.
Example of Multilevel Inheritance in C++ #include<iostream> using namespace std; class B1 { protected: float m, n; public: assign (float x, float y) { m = x; n = y; } }; //end base class B1
Example of Multilevel Inheritance in C++ class B2 : public B1 { public: sum() { cout<<"Sum of "<<m<<"and"<<n<<"="<<m+n<<endl; } }; //end derived class B2 class D1 : public B2 { public: product() { cout<<"Product of "<<m<<"and"<<n<<"="<<m*n<<endl; } }; //end derived class D1
Example of Multilevel Inheritance in C++ main() { D1 x; x .assign (10, 2); x .sum (); x .product(); return 0; }
Output of Program Sum of 10 and 2 = 12 Product of 10 and 2 = 20