-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism.cpp
More file actions
241 lines (197 loc) · 4.55 KB
/
polymorphism.cpp
File metadata and controls
241 lines (197 loc) · 4.55 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// Created by zing on 6/7/2020.
//
#include <iostream>
using namespace std;
//抽象制作饮品
class AbstractDrinking {
public:
//烧水
virtual void Boil() = 0;
//冲泡
virtual void Brew() = 0;
//倒入杯中
virtual void PourInCup() = 0;
//加入辅料
virtual void PutSomething() = 0;
//规定流程
void MakeDrink() {
Boil();
Brew();
PourInCup();
PutSomething();
}
};
//制作咖啡
class Coffee : public AbstractDrinking {
public:
//烧水
virtual void Boil() {
cout << "煮农夫山泉!" << endl;
}
//冲泡
virtual void Brew() {
cout << "冲泡咖啡!" << endl;
}
//倒入杯中
virtual void PourInCup() {
cout << "将咖啡倒入杯中!" << endl;
}
//加入辅料
virtual void PutSomething() {
cout << "加入牛奶!" << endl;
}
};
//制作茶水
class Tea : public AbstractDrinking {
public:
//烧水
virtual void Boil() {
cout << "煮自来水!" << endl;
}
//冲泡
virtual void Brew() {
cout << "冲泡茶叶!" << endl;
}
//倒入杯中
virtual void PourInCup() {
cout << "将茶水倒入杯中!" << endl;
}
//加入辅料
virtual void PutSomething() {
cout << "加入枸杞!" << endl;
}
};
//业务函数
void DoWork(AbstractDrinking *drink) {
drink->MakeDrink();
delete drink;
}
void test01() {
DoWork(new Coffee);
cout << "--------------" << endl;
DoWork(new Tea);
}
//抽象CPU类
class CPU {
public:
//抽象的计算函数
virtual void calculate() = 0;
};
//抽象显卡类
class VideoCard {
public:
//抽象的显示函数
virtual void display() = 0;
};
//抽象内存条类
class Memory {
public:
//抽象的存储函数
virtual void storage() = 0;
};
//电脑类
class Computer {
public:
Computer(CPU *cpu, VideoCard *vc, Memory *mem) {
m_cpu = cpu;
m_vc = vc;
m_mem = mem;
}
//提供工作的函数
void work() {
//让零件工作起来,调用接口
m_cpu->calculate();
m_vc->display();
m_mem->storage();
}
//提供析构函数 释放3个电脑零件
~Computer() {
//释放CPU零件
if (m_cpu != NULL) {
delete m_cpu;
m_cpu = NULL;
}
//释放显卡零件
if (m_vc != NULL) {
delete m_vc;
m_vc = NULL;
}
//释放内存条零件
if (m_mem != NULL) {
delete m_mem;
m_mem = NULL;
}
}
private:
CPU *m_cpu; //CPU的零件指针
VideoCard *m_vc; //显卡零件指针
Memory *m_mem; //内存条零件指针
};
//具体厂商
//Intel厂商
class IntelCPU : public CPU {
public:
virtual void calculate() {
cout << "Intel的CPU开始计算了!" << endl;
}
};
class IntelVideoCard : public VideoCard {
public:
virtual void display() {
cout << "Intel的显卡开始显示了!" << endl;
}
};
class IntelMemory : public Memory {
public:
virtual void storage() {
cout << "Intel的内存条开始存储了!" << endl;
}
};
//Lenovo厂商
class LenovoCPU : public CPU {
public:
virtual void calculate() {
cout << "Lenovo的CPU开始计算了!" << endl;
}
};
class LenovoVideoCard : public VideoCard {
public:
virtual void display() {
cout << "Lenovo的显卡开始显示了!" << endl;
}
};
class LenovoMemory : public Memory {
public:
virtual void storage() {
cout << "Lenovo的内存条开始存储了!" << endl;
}
};
void test02() {
//第一台电脑零件
CPU *intelCpu = new IntelCPU;
VideoCard *intelCard = new IntelVideoCard;
Memory *intelMem = new IntelMemory;
cout << "第一台电脑开始工作:" << endl;
//创建第一台电脑
Computer *computer1 = new Computer(intelCpu, intelCard, intelMem);
computer1->work();
delete computer1;
cout << "-----------------------" << endl;
cout << "第二台电脑开始工作:" << endl;
//第二台电脑组装
Computer *computer2 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);;
computer2->work();
delete computer2;
cout << "-----------------------" << endl;
cout << "第三台电脑开始工作:" << endl;
//第三台电脑组装
Computer *computer3 = new Computer(new LenovoCPU, new IntelVideoCard, new LenovoMemory);;
computer3->work();
delete computer3;
}
int main() {
test01();
test02();
return 0;
}