C++ Program using hierarchical inheritance


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


class A { }

class B : public A { }

class C : public A { }


Example: How to implement hierarchical inheritance in C++

Run it Now
#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