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
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
Diagrammatically
Natural inheritence
Multi level inheritence

Multi level inheritence

  • 1.
    When a classis 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
  • 2.
    Child class The classwhich 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.
  • 3.
    Syntax of MultilevelInheritance class A //Base class { --------- --------- //body of class A --------- }; class B : public A //B derived from class A { ---------- --------- //body of class B ---------- };
  • 4.
    Syntax of MultilevelInheritance class C : public B //C derived from class B { ---------- --------- //body of class C ---------- }; //This process can be extended to any number of levels.
  • 5.
    Example of MultilevelInheritance 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
  • 6.
    Example of MultilevelInheritance 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
  • 7.
    Example of MultilevelInheritance in C++ main() { D1 x; x .assign (10, 2); x .sum (); x .product(); return 0; }
  • 8.
    Output of Program •Sum of 10 and 2 = 12 • Product of 10 and 2 = 20
  • 9.
  • 10.