CPP Multilevel Inheritance

Multilevel Inheritance

  • In C++ programming, a class be can derived from a derived class which is known as multilevel inhertiance.
  • In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class.
  • The Super class for one, is sub class for the other.

Syntax:

class A
{ .... ... .... };
class B : public A
{ .... ... .... };
class C : public B
{ .... ... .... };

Example:

#include <iostream>

class A
{
    public:
      void display()
      {
          cout<<"Base class content.";
      }
};

class B : public A
{

};

class C : public B
{
 
};

void main()
{
    C c;
    c.display();
   getch();
}

Output:

Base class content.
  • In this program, class B is derived from A and C is derived from B. An object of class C is defined in main() function.
  • When the display() function is called, display() in class A is executed because there is no display() function inC and B.
  • The program first looks for display() in class C first but, can't find it. 
  • Then, looks in B because C is derived from B. Again it can't find it. And finally looks it in A and executes the codes inside that function.


Share This Page to Download:

Share to Download

Share This Page on:


Ask Question