106: #include <iostream>
107: #include <iomanip>
108: #include <string>
110: using namespace std;
111:
112: #include "ccc_time.h"
113:
114: class Clock {
116: public:
117: /**
118: Constructs a clock that can tell the local time.
119: @param use_military true if the clock uses military format
120: */
121: Clock(bool use_military);
123: /**
124: Gets the location of this clock.
125: @return the location
126: */
127: string get_location() const;
129: /**
130: Gets the hours of this clock.
131: @return the hours, in military or am/pm format
132: */
133: int get_hours() const;
135: /**
136: Gets the minutes of this clock.
137: @return the minutes
138: */
139: int get_minutes() const;
141: /**
142: Checks whether this clock uses miltary format.
143: @return true if miltary format
144: */
145: bool is_military() const;
146: private:
147: bool military;
148: };
149:
150: Clock::Clock(bool use_military)
151: { military = use_military; }
154:
155: string Clock::get_location() const
156: { return "Local"; }
159:
160: int Clock::get_hours() const
161: { Time now;
163: int hours = now.get_hours();
164: if (military) return hours;
165: if (hours == 0) return 12;
167: else if (hours > 12) return hours - 12;
169: else return hours;
171: }
172:
173: int Clock::get_minutes() const
174: { Time now;
176: return now.get_minutes();
177: }
178:
179: bool Clock::is_military() const
180: { return military; }
183:
184: class TravelClock : public Clock {
186: public:
187: /**
188: Constructs a travel clock that can tell the time
189: at a specified location
190: @param mil true if the clock uses military format
191: @param loc the location
192: @param diff the time difference from the local time
193: */
194: TravelClock(bool mil, string loc, int diff);
195: string get_location() const;
196: int get_hours() const;
197: private:
198: string location;
199: int time_difference;
200: };
201:
202: TravelClock::TravelClock(bool mil, string loc, int diff)
203: : Clock(mil)
204: { location = loc;
206: time_difference = diff;
207: while (time_difference < 0)
208: time_difference = time_difference + 24;
209: }
210:
211: string TravelClock::get_location() const
212: { return location; }
215:
216: int TravelClock::get_hours() const
217: { int h = Clock::get_hours();
219: if (is_military())
220: return (h + time_difference) % 24;
221: else
222: { h = (h + time_difference) % 12;
224: if (h == 0) return 12;
225: else return h;
226: }
227: }
228:
229: int main()
230: { Clock clock1(true);
232: TravelClock clock2(true, "Rome", 9);
233: TravelClock clock3(false, "Tokyo", -7);
234:
235: cout << clock1.get_location() << " time: "
236: << clock1.get_hours() << ":"
237: << setw(2) << setfill('0')
238: << clock1.get_minutes()
239: << setfill(' ') << "\n";
240: cout << clock2.get_location() << " time: "
241: << clock2.get_hours() << ":"
242: << setw(2) << setfill('0')
243: << clock2.get_minutes()
244: << setfill(' ') << "\n";
245: cout << clock3.get_location() << " time: "
246: << clock3.get_hours() << ":"
247: << setw(2) << setfill('0')
248: << clock3.get_minutes()
249: << setfill(' ') << "\n";
250: return 0;
251: }