-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.cpp
More file actions
73 lines (61 loc) · 1.92 KB
/
adapter.cpp
File metadata and controls
73 lines (61 loc) · 1.92 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
// Have to match up round hole with square peg via an adapter
#include <iostream>
#include <cmath>
class RoundPeg {
public:
RoundPeg(int radius) : radius_{radius} {}
~RoundPeg() {}
int getRadius() const {
return radius_;
}
private:
int radius_;
};
class SquarePeg {
public:
SquarePeg(int width) : width_{width} {}
int getWidth() {
return width_;
}
private:
int width_;
};
class RoundHole {
private:
int radius_;
public:
RoundHole(int radius) : radius_{radius} { }
int getRadius() const {
return radius_;
}
bool fits(RoundPeg* peg) { // only a round peg can fit into a round hole!
return this->getRadius() >= peg->getRadius();
}
};
class SquarePegAdapter : public RoundPeg {
private:
SquarePeg* peg_;
public:
// constructor is special - roundPeg has no default constructor
// so but you need to call a roundPeg constructor to create SquarePegAdapter
// that means
SquarePegAdapter(SquarePeg* peg) : RoundPeg(peg->getWidth()), peg_(peg) {}
int getRadius() {
// output is round peg that could fit in the square input
return peg_->getWidth() * sqrt(2) / 2;
}
};
int main() {
RoundHole* hole = new RoundHole(5);
RoundPeg* rpeg = new RoundPeg(5);
std::cout << (hole->fits(rpeg) ? "fits" : "doesn't fit") << std::endl;
SquarePeg* small_sqpeg = new SquarePeg(5);
SquarePeg* large_sqpeg = new SquarePeg(10);
// below line doesn't compile....
// std::cout << (hole->fits(small_sqpeg) ? "fits" : "doesn't fit") << std::endl;
SquarePegAdapter* small_sqpeg_adapter = new SquarePegAdapter(small_sqpeg);
SquarePegAdapter* large_sqpeg_adapter = new SquarePegAdapter(large_sqpeg);
std::cout << (hole->fits(small_sqpeg_adapter) ? "fits" : "doesn't fit") << std::endl;
std::cout << (hole->fits(large_sqpeg_adapter) ? "fits" : "doesn't fit") << std::endl;
return 0;
}