forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hybrid_Inheritance(Diamond_Problem).cpp
81 lines (71 loc) · 1.21 KB
/
Hybrid_Inheritance(Diamond_Problem).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
using namespace std;
class A
{
private:
int A=10;
public:
void displayA()
{
cout << "Display A" <<endl;
cout << "A= " <<A << endl;
}
};
class B : public A
{
private:
int B=20;
public:
void displayB()
{
cout << "Display B" <<endl;
cout <<"B= " << B << endl;
}
};
class C : public A
{
private:
int C=30;
public:
void displayC()
{
cout << "Display C" <<endl;
cout << "C= "<< C << endl;
}
};
class D : public B , public C
{
private:
int D=40;
public:
void displayD()
{
cout<< "Display D" <<endl;
cout << "D= "<< D << endl;
}
};
int main()
{
A Obj1;
Obj1.displayA();
B Obj2;
Obj2.displayB();
Obj2.displayA();
C Obj3;
Obj3.displayC();
Obj3.displayA();
D Obj4;
Obj4.displayD();
Obj4.displayB();
Obj4.displayC();
// Obj4.DisplayA(); ---> Diamond Problem (Ambiguious)
/*
What is Diamond Problem?
In case of hybrid inheritance if there is a scenario like
that
if there is a parent(class A) and that parent has 2 children(class B,C) and again
2 children are the parent of another class(Class D),then
Diamond Problem Occurs.
*/
return 0;
}