Simple Program for Hierarchical Inheritance in C++


In the Hierarchical Inheritance, there is only base class and all the derived classes are derived from this base class. The following is the schematic representation. Class A is the base class and class B and class C are the derived classes. Two classes are derived from the base class. Have a look at the following example.

Simple Example of Hierarchical Inheritance in C++

#include<iostream> using namespace std; class base{ public: void rbc1(){ cout<<"base - rbc1"<<endl; } }; class der1 : public base{ public: void rbc2(){ cout<<"der1 - rbc2"<<endl; } }; class der2 : public base{ public: void rbc3(){ cout<<"der2 - rbc3"<<endl; } }; int main(){ base b; b.rbc1(); der1 d1; d1.rbc1(); d1.rbc2(); der2 d2; d2.rbc1(); d2.rbc3(); return 0; }

Output:-

base - rbc1
base - rbc1
der1 - rbc2
base - rbc1
der2 - rbc3