-
Notifications
You must be signed in to change notification settings - Fork 2
/
T47.cpp
87 lines (67 loc) · 1.63 KB
/
T47.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
82
83
84
85
86
87
//C++多继承
//Java语言不允许多继承,多继承有歧义。
#include <iostream>
using namespace std;
class BaseActivity {
public:
void onCreate() {
cout << "BaseActivity onCreate" << endl;
}
void onStart() {
cout << "BaseActivity onStart" << endl;
}
void show() {
cout << "BaseActivity show" << endl;
}
};
class BaseActivity2 {
public:
void onCreate() {
cout << "BaseActivity2 onCreate" << endl;
}
void onStart() {
cout << "BaseActivity2 onStart" << endl;
}
void show() {
cout << "BaseActivity2 show" << endl;
}
};
class BaseActivity3 {
public:
void onCreate() {
cout << "BaseActivity3 onCreate" << endl;
}
void onStart() {
cout << "BaseActivity3 onStart" << endl;
}
void show() {
cout << "BaseActivity3 show" << endl;
}
};
//子类继承三个父类
class MainActivity : public BaseActivity, public BaseActivity2, public BaseActivity3 {
public:
void onCreate() {
cout << "MainActivity onCreate" << endl;
}
void onStart() {
cout << "MainActivity onStart" << endl;
}
void show(){
cout << "MainActivity show" << endl;
}
};
int main47() {
MainActivity mainActivity;
mainActivity.onCreate();
mainActivity.onStart();
//函数歧义
// mainActivity.show();
// 解决方案一: 明确指定父类::
mainActivity.BaseActivity::show();
// 解决方案二: 子类覆写父类的show函数
mainActivity.show();
// 解决方案三: 虚基类 虚继承的范畴 virtual修饰
// virtual public BaseActivity
return 0;
}