C++ Program using hybrid inheritance
In this program, You will learn how to implement hybrid inheritance in C++.
class A {
//statement
}
class B : public A {
//statement
}
class C : public A {
//statement
}
class D : public B, C {
//statement
}
Example: How to implement hybrid inheritance in C++.
#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