-
Notifications
You must be signed in to change notification settings - Fork 0
/
DP_Stractural_bridge.cpp
79 lines (69 loc) · 2.02 KB
/
DP_Stractural_bridge.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
#include <iostream>
//abstruct class(House) hold instance of implemented class and has it's own implementations, concatanating 2 implementations.
class House_Implementation {
public:
virtual void createDoor()=0;
virtual void createWall()=0;
};
class Wood_Implementation: public House_Implementation {
void createDoor()override {
std::cout<<"Door builded from wood\n";
}
void createWall()override {
std::cout << "Wall builded from wood\n";
}
};
class Brick_Implementation: public House_Implementation {
void createDoor()override {
std::cout << "Door builded from bricks\n";
}
void createWall()override {
std::cout << "Wall builded from bricks\n";
}
};
class House {
protected:
House_Implementation* H_I;
public:
House(House_Implementation* h_i) { //constructor
H_I = h_i;
}
virtual void build() = 0; //virtual => abstruct class
void open_door() {
std::cout << "Door is opened\n";
}
void close_door() {
std::cout << "Door is closed\n";
}
};
class Big_House:public House {
public:
Big_House(House_Implementation* specific_H_I) :House(specific_H_I) {
//H_I = specific_H_I; //as it do the same, House constructor needed by cpp for my understanding.
}
void build() override {
std::cout << "What a big house! I like it, but I need a door and some more walls here.\n";
H_I->createDoor();
H_I->createWall();
}
};
class Small_House :public House {
public:
Small_House(House_Implementation* specific_H_I):House(specific_H_I) {
H_I = specific_H_I;
}
void build() override {
std::cout << "What a small cosy house! I like it, but I need a door and some more walls here.\n";
H_I->createDoor();
H_I->createWall();
}
};
int main() {
House* my_big_house = new Big_House(new Brick_Implementation);
my_big_house->build();
my_big_house->close_door();
House* my_house = new Small_House(new Wood_Implementation);
my_house->build();
delete (my_big_house); //delete is needed per pointer, not like that - delete(a,b);
delete(my_house);
}