C++ Program using hierarchical inheritance


In this program, You will learn how to implement hierarchical inheritance in C++.


class A { 
   //statement
}

class B : public A { 
  //statement
}

class C : public A { 
  //statement
}

Example: How to implement hierarchical inheritance in C++.

#include<iostream>
using namespace std;

class A {
public:
   int x;

   A() {
       x = 10;
   }
};

class B : public A {
public:

   void display() {
       cout << "The value of x into B class is:" << x;
   }
};

class C : public A {
public:

   void display() {
       cout << "\nThe value of x into C class is:" << x;
   }
};

int main() {

   B obj1;
   obj1.display();

   C obj2;
   obj2.display();

   return 0;
}

Output:

The value of x into B class is:10
The value of x into C class is:10