-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmi-diamond.cpp
57 lines (45 loc) · 1011 Bytes
/
mi-diamond.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
// #define PROBLEM
#define FIX
#ifdef PROBLEM
#include <iostream>
using namespace std;
class Base {
protected:
int iData;
public:
Base() { iData = 10; }
};
class Derived1 : public Base { };
class Derived2 : public Base { };
class Derived3 : public Derived1, public Derived2 {
public:
int GetData() { return iData; }
};
int main (int argc, char**argv)
{
Derived3 obj;
cout << obj.GetData() << endl;
}
#endif
#ifdef FIX
// EXAMPLE: Demonstrate the usage of virtual base class in the Diamond problem to fix the compilation error
#include <iostream>
using namespace std;
class Base {
protected:
int iData;
public:
Base() { iData = 10; }
};
class Derived1 : virtual public Base { };
class Derived2 : virtual public Base { };
class Derived3 : public Derived1, public Derived2 {
public:
int GetData() { return iData; }
};
int main (int argc, char**argv)
{
Derived3 obj;
cout << obj.GetData() << endl;
}
#endif