Hierarchical Inheritance in C++

More than one derived classes are created from a single base class, is called Hierarchical Inheritance.

Syntax:

Class Base{

   //body of Base class

}

class derived1:_public Base

{

   //body of derived1 class

}

class derived2:_public Base

{

   //body of derived2 class

}

Example:

#include<iostream>

using namespace std;

class Parent{     // Base class

               public:

                    Parent(){

 

                               cout<< " This is Parent class " <<endl;

                     }

}; 

class Child1:public Parent{         // derived1  class  --> Base class

                public:

                   Child1(){

                             cout<< " This is Child1 class " <<endl;

                  }

};

class Child2:public Parent{        // derived2  class  --> Base class

                 public:

                    Child2(){

                               cout<< " This is Child2 class " <<endl;

                     }

};       

int main()

{

Child1 obj1;

Child2 obj2;

}

Output:

This is Parent class

This is Child1 class

This is Parent class

This is Child2 class

 

 

Post a Comment

0 Comments

Close Menu