-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathocp.cpp
More file actions
56 lines (46 loc) · 1.08 KB
/
ocp.cpp
File metadata and controls
56 lines (46 loc) · 1.08 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
//
// Created by Nelson Lin.
// The Open Closed Principle
//
#include <iostream>
#include <vector>
using namespace std;
struct canshoot {
virtual void shoot() = 0;
virtual ~canshoot() {}
};
struct laserbean : canshoot {
void shoot() override {
cout << "Ziiip" << endl;
}
};
struct cesiumBeam : canshoot {
void shoot() override {
cout << "Ciiiip" << endl;
}
};
struct rocketLauncher : canshoot {
void shoot() override {
cout << "Woosh" << endl;
}
};
struct weapon_composite {
weapon_composite(vector<unique_ptr<canshoot>> &weapons) {
for(auto& weapon : weapons)
weapon->shoot();
}
};
int main() {
unique_ptr<canshoot> c = make_unique<laserbean>();
vector<unique_ptr<canshoot>> data;
data.emplace_back(move(c));
weapon_composite wc{data};
for(int i=0; i < 33; ++i)
cout << "=";
cout << "\n3 weapons fire" << endl;
data.emplace_back(make_unique<rocketLauncher>());
data.emplace_back(make_unique<cesiumBeam>());
weapon_composite wc2{data};
getchar();
return 0;
}