Header Ads Widget

Ticker

6/recent/ticker-posts

C++ Hierarchical Inheritance Note & Coding - Digitalise Data

 

"Digitalise Data" , "C++ Hierarchical Inheritance"

When several classes are derived from common base class it is called hierarchical inheritance.


In C++ hierarchical inheritance, the feature of the base class is inherited onto more than one sub-class.

For example, a car is a common class from which Audi, Ferrari, Maruti etc can be derived.

Following block diagram highlights its concept.
C++ Hierarchical Inheritance Block Diagram
c++ hierarchical inheritance block diagram

As shown in above block diagram, in C++ hierarchical inheritance all the derived classes have common base class. The base class includes all the features that are common to derived classes.

As in other inheritance, based on the visibility mode used or access specifier used while deriving, the properties of the base class are derived. Access specifier can be private, protected or public.


C++ Hierarchical Inheritance Syntax

class A // base class
{
    ..............
};
class B : access_specifier A // derived class from A
{
    ...........
 } ;
 class C : access_specifier A // derived class from A
 {
    ...........
 } ;
 class D : access_specifier A // derived class from A
 {
     ...........
 } ;

--------------------------------------------------------
Example Of Hierarchical Inheritance 

#include <iostream.h>
#include <conio.h>
class A {
public:
  A(){
     cout<<"Constructor of A class"<<endl;
  }
};
class B: public A {
public:
  B(){ 
     cout<<"Constructor of B class"<<endl;
  }
};
class C: public A{
public:
  C(){
     cout<<"Constructor of C class"<<endl;
  }
};
void main() {
   //Creating object of class C
   clrscr();
   C obj;
   B obj1;
   getch();
}

--------------------------------------------------------
Output

Constructor of A class
Constructor of C class
--------------------------------------------------------

Post a Comment

0 Comments