C++ Program using hybrid inheritance


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


class A { }

class B : public A { }

class C : public A { }

class D : public B, C { }


Example: How to implement hybrid inheritance in C++

Run it Now
#include<iostream>
using namespace std;

class A {
public:
   int x, y, z;
   A() {
       x = 20;
       y = 10;
   }
};

class B : public A {
public:
   void addition() {
       z = x + y;
       cout << "Addition is :" << z;
   }
};

class C : public A {
public:
   void subtraction() {
       z = x - y;
       cout << "\nSubtraction is :" << z;
   }
};

class D : public B, C {
public:
   void display() {
       addition();
       subtraction();
   }
};

int main() {
   D obj;
   obj.display();

   return 0;
}

Output:

Addition is :30
Subtraction is :10