253: #include <iostream>
254: #include <iomanip>
255: #include <string>
256: #include <vector>
257: 
258: using namespace std;
259: 
260: #include "ccc_time.h"
261: 
262: class Clock
263: {
264: public:
265:    /**
266:       Constructs a clock that can tell the local time.
267:       @param use_military true if the clock uses military format
268:    */
269:    Clock(bool use_military);
270:    
271:    /**
272:       Gets the location of this clock.
273:       @return the location
274:    */
275:    virtual string get_location() const;
276: 
277:    /**
278:       Gets the hours of this clock.
279:       @return the hours, in military or am/pm format
280:    */
281:    virtual int get_hours() const;
282: 
283:    /**
284:       Gets the minutes of this clock.
285:       @return the minutes
286:    */
287:    int get_minutes() const;
288: 
289:    /**
290:       Checks whether this clock uses miltary format.
291:       @return true if miltary format
292:    */
293:    bool is_military() const;
294: private:
295:    bool military;
296: };
297: 
298: Clock::Clock(bool use_military)
299: {
300:    military = use_military;
301: }
302: 
303: string Clock::get_location() const
304: {
305:    return "Local";
306: }
307: 
308: int Clock::get_hours() const
309: {
310:    Time now;
311:    int hours = now.get_hours();
312:    if (military) return hours;
313:    if (hours == 0) 
314:       return 12;
315:    else if (hours > 12)
316:       return hours - 12;
317:    else
318:       return hours;
319: }
320: 
321: int Clock::get_minutes() const
322: {
323:    Time now;
324:    return now.get_minutes();
325: }
326: 
327: bool Clock::is_military() const
328: {
329:    return military;
330: }
331: 
332: class TravelClock : public Clock
333: {
334: public:
335:    /**
336:       Constructs a travel clock that can tell the time
337:       at a specified location
338:       @param mil true if the clock uses military format
339:       @param loc the location
340:       @param diff the time difference from the local time
341:    */
342:    TravelClock(bool mil, string loc, int diff);
343:    string get_location() const;
344:    int get_hours() const;
345: private:
346:    string location;
347:    int time_difference;
348: };
349: 
350: TravelClock::TravelClock(bool mil, string loc, int diff)
351:    : Clock(mil)
352: {
353:    location = loc;
354:    time_difference = diff;
355:    while (time_difference < 0) 
356:       time_difference = time_difference + 24;
357: }
358: 
359: string TravelClock::get_location() const
360: {
361:    return location;
362: }
363: 
364: int TravelClock::get_hours() const
365: {
366:    int h = Clock::get_hours();
367:    if (is_military())
368:       return (h + time_difference) % 24;
369:    else
370:    {
371:       h = (h + time_difference) % 12;
372:       if (h == 0) return 12;
373:       else return h;
374:    }
375: }
376: 
377: int main()
378: {
379:    vector<Clock*> clocks(3);
380:    clocks[0] = new Clock(true);
381:    clocks[1] = new TravelClock(true, "Rome", 9);
382:    clocks[2] = new TravelClock(false, "Tokyo", -7);
383: 
384:    for (int i = 0; i < clocks.size(); i++)
385:    {
386:       cout << clocks[i]->get_location() << " time: " 
387:          << clocks[i]->get_hours() << ":"
388:          << setw(2) << setfill('0') 
389:          << clocks[i]->get_minutes() 
390:          << setfill(' ') << "\n";
391:    }
392:    return 0;
393: }
394: