    106: #include <iostream>
    107: #include <iomanip>
    108: #include <string>
    109:
    110: using namespace std;
    111:
    112: #include "ccc_time.h"
    113:
    114: class Clock
    115: {
    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);
    122:
    123:    /**
    124:       Gets the location of this clock.
    125:       @return the location
    126:    */
    127:    string get_location() const;
    128:
    129:    /**
    130:       Gets the hours of this clock.
    131:       @return the hours, in military or am/pm format
    132:    */
    133:    int get_hours() const;
    134:
    135:    /**
    136:       Gets the minutes of this clock.
    137:       @return the minutes
    138:    */
    139:    int get_minutes() const;
    140:
    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: {
    152:    military = use_military;
    153: }
    154:
    155: string Clock::get_location() const
    156: {
    157:    return "Local";
    158: }
    159:
    160: int Clock::get_hours() const
    161: {
    162:    Time now;
    163:    int hours = now.get_hours();
    164:    if (military) return hours;
    165:    if (hours == 0)
    166:       return 12;
    167:    else if (hours > 12)
    168:       return hours - 12;
    169:    else
    170:       return hours;
    171: }
    172:
    173: int Clock::get_minutes() const
    174: {
    175:    Time now;
    176:    return now.get_minutes();
    177: }
    178:
    179: bool Clock::is_military() const
    180: {
    181:    return military;
    182: }
    183:
    184: class TravelClock : public Clock
    185: {
    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: {
    205:    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: {
    213:    return location;
    214: }
    215:
    216: int TravelClock::get_hours() const
    217: {
    218:    int h = Clock::get_hours();
    219:    if (is_military())
    220:       return (h + time_difference) % 24;
    221:    else
    222:    {
    223:       h = (h + time_difference) % 12;
    224:       if (h == 0) return 12;
    225:       else return h;
    226:    }
    227: }
    228:
    229: int main()
    230: {
    231:    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: }
    252: