-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathT50.cpp
88 lines (63 loc) · 1.92 KB
/
T50.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
88
//C++多态 (虚函数)
// 静态多态【编译器确定调用哪个函数】
// 动态多态【程序在运行期间才能确定调用函数的真实对象】
// Java语言默认支持多态
// C++默认关闭多态,怎么开启多态?虚函数【在父类上给函数增加virtual关键字】
#include <iostream>
using namespace std;
class BaseActivity {
public:
//虚函数 实现C++多态功能
virtual void onStart() {
cout << "BaseActivity onStart" << endl;
}
};
class HomeActivity : public BaseActivity {
public:
void onStart() {//重写父类函数
cout << "HomeActivity onStart" << endl;
}
};
class LoginActivity : public BaseActivity {
public:
void onStart() {
cout << "LoginActivity onStart" << endl;
}
};
//在此函数体现多态,根据传入参数执行
void startToActivity(BaseActivity *baseActivity) {
baseActivity->onStart();
}
void add1(int number1, int number2) {
}
void add1(float number1, float number2) {
}
int main50() {
//TODO 第一版本
HomeActivity *homeActivity = new HomeActivity();
LoginActivity *loginActivity = new LoginActivity();
startToActivity(homeActivity);
startToActivity(loginActivity);
if (homeActivity || loginActivity) {
delete homeActivity;
delete loginActivity;
}
cout << endl;
//TODO 第二版本
BaseActivity *activity1 = new HomeActivity();
BaseActivity *activity2 = new LoginActivity();
startToActivity(activity1);
startToActivity(activity2);
if (activity1 || activity2) {
delete activity1;
delete activity2;
}
cout << endl;
//TODO 第三版本
//在父类函数上增加virtual变为虚函数,实现C++多态功能
//什么是多态? 父类引用指向子类对象,同一个方法有不同实现,重写和重载
//静态多态,方法重载
add1(100, 200);
add1(10.1f, 23.33f);
return 0;
}