forked from muziing/CPP_Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
49_类与对象_多态_06.cpp
152 lines (134 loc) · 2.48 KB
/
49_类与对象_多态_06.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
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
#include <iostream>
using namespace std;
// 多态 - 案例 - 电脑组装
// 抽象不同零件类
// 抽象CPU类
class CPU
{
public:
// 抽象的计算函数
virtual void calculate() = 0;
};
// 抽象GPU类
class GPU
{
public:
// 抽象的显示函数
virtual void display() = 0;
};
// 抽象内存类
class Memory
{
public:
// 抽象的存储函数
virtual void storage() = 0;
};
class Computer
{
public:
Computer(CPU *cpu, GPU *gpu, Memory *mem)
{
m_cpu = cpu;
m_gpu = gpu;
m_mem = mem;
}
~Computer()
{
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
if (m_gpu != NULL)
{
delete m_gpu;
m_gpu = NULL;
}
if (m_mem != NULL)
{
delete m_mem;
m_mem = NULL;
}
}
// 提供工作函数
void work()
{
// 调用接口,让零件工作起来
m_cpu->calculate();
m_gpu->display();
m_mem->storage();
}
private:
CPU *m_cpu; // CPU的零件指针
GPU *m_gpu; // 显卡零件指针
Memory *m_mem; //内存条零件指针
};
// 具体厂商
class IntelCPU : public CPU
{
public:
virtual void calculate()
{
cout << "Intel的CPU开始计算了" << endl;
}
};
class IntelGPU : public GPU
{
public:
virtual void display()
{
cout << "Intel的GPU开始显示了" << endl;
}
};
class IntelMemory : public Memory
{
public:
virtual void storage()
{
cout << "Intel的内存开始存储了" << endl;
}
};
class AMDCPU : public CPU
{
public:
virtual void calculate()
{
cout << "AMD的CPU开始计算了" << endl;
}
};
class AMDGPU : public GPU
{
public:
virtual void display()
{
cout << "AMD的GPU开始显示了" << endl;
}
};
class AMDMemory : public Memory
{
public:
virtual void storage()
{
cout << "AMD的内存开始存储了" << endl;
}
};
void test01()
{
// 第一台电脑零件
CPU *intelCpu = new IntelCPU;
GPU *intelGpu = new IntelGPU;
Memory *intelMem = new IntelMemory;
// 创建第一台电脑
Computer *computer1 = new Computer(intelCpu, intelGpu, intelMem);
computer1->work();
delete computer1;
// 创建第一台电脑
Computer *computer2 = new Computer(new AMDCPU, new AMDGPU, new AMDMemory);
computer2->work();
delete computer2;
}
int main()
{
test01();
return 0;
}