-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.cpp
More file actions
44 lines (34 loc) · 733 Bytes
/
adapter.cpp
File metadata and controls
44 lines (34 loc) · 733 Bytes
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
#include <memory>
class Lion {
public:
virtual void roar(void) = 0;
};
class AfricanLion : public Lion {
public:
void roar(void) override {}
};
class AsianLion : public Lion {
public:
void roar(void) override {}
};
class Hunter {
public:
void hunt(Lion &lion) { lion.roar(); }
};
class WildDog {
public:
void bark(void) {}
};
class WildDogAdapter : public Lion {
private:
std::shared_ptr<WildDog> m_Dog;
public:
WildDogAdapter(const std::shared_ptr<WildDog> &dog) : m_Dog(dog) {}
void roar(void) override { m_Dog->bark(); }
};
int main(void) {
auto wildDog = std::make_shared<WildDog>();
auto wildDogAdapter = WildDogAdapter(wildDog);
Hunter hunter = Hunter();
hunter.hunt(wildDogAdapter);
}