-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite.cpp
43 lines (36 loc) · 868 Bytes
/
composite.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
#include <iostream>
#include <string>
#include <utility>
#include <vector>
struct Shape {
virtual void draw(std::string fillColor) = 0;
};
struct Square: public Shape {
void draw(std::string fillColor) override {
std::cout << "Drawing a square with color " << fillColor << std::endl;
}
};
struct Circle: public Shape {
void draw(std::string fillColor) override {
std::cout << "Drawing a circle with color " << fillColor << std::endl;
}
};
class Canvas: public Shape {
public:
Canvas(std::vector<Shape *> shapes): shapes(std::move(shapes)) {}
virtual ~Canvas() {
for (auto &shape: shapes)
delete shape;
}
void draw(std::string fillColor) override {
for (auto &shape: shapes)
shape->draw(fillColor);
}
private:
std::vector<Shape *> shapes;
};
int main() {
auto canvas = Canvas({new Circle, new Square});
canvas.draw("red");
return 0;
}