-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInheritance_Multiple_inheritance.cpp
61 lines (54 loc) · 1.3 KB
/
Inheritance_Multiple_inheritance.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
//programming for multiple inheritancea and its ambiguty
// author: jaydattpatel
#include<iostream>
using namespace std;
class A
{
public:
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public A, public B // Note the order
{
public:
C() { cout << "C's constructor called" << endl; }
};
//--Ambiguty in multiple inheritance--------------------------------------------------
class X
{
protected:
int a=5;
public:
X() { cout << "X's constructor called" << endl; }
};
class Y: virtual public X
{
public:
Y() { cout << "Y's constructor called" << endl; }
};
class Z: virtual public X
{
public:
Z() { cout << "Z's constructor called" << endl; }
};
class P: public Y, public Z //here Y and Z both is derived from base class X so virtual key is used for clearity
{
public:
P() { cout << "P's constructor called" << endl; }
int fun()
{return a;}
};
//---------------------------------------------------
int main()
{
cout<<"----------------------------\n";
C c;
cout<<"----------------------------\n";
P p;
cout<<"a: "<<p.fun();
return 0;
}