Hierarchical Inheritance in C++

In "Hierarchical Inheritance" their is only one Parent class and 'n' numbers of  Child class.

LOGO


Basically their is only one Parent class which have a lot of Child class sharing the same properties. This type of inheritance is used when we need to share the same type of behavior / properties / functions to a number of  different classes.

Block-Diagram


Syntax:-
class Parent
{
//Body of the Parent class
};
class Child1 : access_specifier Parent
{
//Body of the Child1 class
};
class Child2 : access_specifier Parent
{
//Body of the Child2 class
};
class Child3 : access_specifier Parent
{
//Body of the Child3 class
};
class Child4 : access_specifier Parent
{
//Body of the Child4 class
};


Example of Hierarchical Inheritance
#include<iostream>
using namespace std;

class Name
{
 protected:
  char name[20]="Mr C";
};

class Marks:public Name
{
 public:
  int total_marks=90;
  void put()
  {
   cout<< name<<" have got Total marks: "<<total_marks;
   cout<<endl;
  }
};

class Academic_Details:public Name
{
 public:
  int Redg_no=123456;
  void put()
  {
   cout<<name <<" have the Registration number: "<< Redg_no;
  }
};

int main()
{
 Marks x;
 Academic_Details y;
 x.put();
 y.put();
}


For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...

Post a Comment

0 Comments