-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.cpp
More file actions
76 lines (61 loc) · 1.77 KB
/
template.cpp
File metadata and controls
76 lines (61 loc) · 1.77 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
#include <iostream>
using namespace std;
//模板模式
/*
* 定义一个算法流程,将一些特定步骤的具体实现、延迟到子类。使得可以在不改变算法流程的情况下,通过不同的子类、来实现“定制”流程中的特定的步骤
*/
class Computer {
public:
void product() {
installCpu();
installRam();
installGraphicsCard();
installHardisk();
}
virtual ~Computer() = default;
protected:
//将抽象行为放到子类实现
virtual void installCpu() = 0;
virtual void installRam() = 0;
virtual void installGraphicsCard() = 0;
private:
//公共的部分,不变
void installHardisk() {
std::cout << "Computer install 1TB hard disk !" << std::endl;
};
};
//子类实现父类所定义的一个或多个抽象方法
class ComputerA : public Computer {
protected:
void installCpu() override {
cout << "ComputerA install Inter Core i5" << endl;
}
void installRam() override {
cout << "ComputerA install 2G Ram" << endl;
}
void installGraphicsCard() override {
cout << "ComputerA install Gtx940 GraphicsCard" << endl;
}
};
class ComputerB : public Computer {
protected:
void installCpu() override {
cout << "ComputerB install Inter Core i7" << endl;
}
void installRam() override {
cout << "ComputerB install 4G Ram" << endl;
}
void installGraphicsCard() override {
cout << "ComputerB install Gtx960 GraphicsCard" << endl;
}
};
//int main() {
// std::shared_ptr<Computer> pComputerA = std::make_shared<ComputerA>();
// pComputerA->product();
//
// //C++14才引入进make_unique
// std::unique_ptr<ComputerB> pComputerB = std::unique_ptr<ComputerB>(new ComputerB);
// pComputerB->product();
//
// return 0;
//}