-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bridge.cpp
143 lines (106 loc) · 2.18 KB
/
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <string.h>
#include <ostream>
#include <algorithm>
using std::cout;
using std::endl;
using std::setw;
using std::setfill;
using std::for_each;
using std::begin;
using std::end;
class TimeImp {
public:
TimeImp(int hr, int min)
: hr_{hr},
min_{min}
{}
virtual auto Tell()->void {
cout << "time is " << setw(2) << setfill(' ') << hr_ << min_ << endl;
}
virtual ~TimeImp() = default;
protected:
int hr_, min_;
};
class CivilianTimeImp
: public TimeImp {
public:
CivilianTimeImp(int hr, int min, int pm)
: TimeImp(hr, min)
{
strcpy(whichM_, pm
? " PM"
: " AM");
}
auto Tell()->void override {
cout << "time is " << hr_ << ":" << min_ << whichM_ << endl;
}
protected:
char whichM_[4];
};
class ZuluTimeImp
: public TimeImp {
public:
ZuluTimeImp(int hr, int min, int zone)
: TimeImp(hr, min)
{
if (zone == 5) strcpy(zone_, " Eastern Standard Time");
else if (zone == 6) strcpy(zone_, " Central Standard Time");
}
auto Tell()->void override {
cout << "time is " << setw(2) << setfill(' ') << hr_ << min_ << zone_ << endl;
}
virtual ~ZuluTimeImp() override = default;
protected:
char zone_[30];
};
class Time {
public:
virtual ~Time() = default;
Time() : imp_{nullptr} {}
Time(int hr, int min) {
imp_ = new TimeImp(hr, min);
}
virtual auto Tell()->void {
imp_->Tell();
}
protected:
TimeImp *imp_;
};
class CivilianTime
: public Time {
public:
CivilianTime(int hr, int min, int pm) {
imp_ = new CivilianTimeImp(hr, min, pm);
}
virtual ~CivilianTime() override = default;
};
class ZuluTime
: public Time {
public:
ZuluTime(int hr, int min, int zone) {
imp_ = new ZuluTimeImp(hr, min, zone);
}
virtual ~ZuluTime() override = default;
};
auto main()->int
{
Time *times[3];
times[0] = new Time(14, 30);
times[1] = new CivilianTime(2, 30, 1);
times[2] = new ZuluTime(14, 30, 6);
for_each(
begin(times), end(times),
[](auto p)
{
p->Tell();
});
cout << endl << endl;
for (auto time : times) {
time->Tell();
}
getchar();
return EXIT_SUCCESS;
}