Practice

Alarm Clock



What is the exact output of the following program?  Show your intermediate work.


 #include <iostream>
 using namespace std;

 class Time {
     int hour;
     int minute;
   public:
     Time() {
         hour = 0;
         minute = 0;
         cout << "T$";
     }
     Time(int h, int m) {
         hour = h;
         minute = m;
         cout << "T*";
     }
     Time(const Time& t) {
         hour = t.hour;
         minute = t.minute;
         cout << "T#";
     }
     ~Time() {
         cout << "T%";
         show();
         cout << endl;
     }
     void operator++(int) {
         minute++;
         if (minute >= 60) {
             minute -= 60;
             hour++;
         }
         if (hour > 12) {
             hour -= 12;
         }
     }
     void show() const {
         cout << hour << ':' << minute << ' ';
     }
     void operator++() {
         minute+=2;
         if (minute >= 60) {
             minute -= 60;
             hour++;
         }
         if (hour > 12) {
             hour -= 12;
         }
     }
     friend bool operator==(const Time& a, const Time& b) {
         return a.hour == b.hour && a.minute == b.minute;
     }
 };

 class Clock {
     Time t;
   public:
     Clock() {
         cout << "C$";
         show();
     }
     Clock(const Time& c) {
         t = c;
         cout << "C*" << endl;
         show();
     }
     Clock(const Clock& c) {
         t = c.t;
         cout << "C#";
         show();
     }
     ~Clock() {
         cout << "C%" << endl;
         show();
     }
     Time time() const { return t; }
     void show() const {
         cout << "T:";
         t.show();
         cout << endl;
     }
     void tick() { t++; }
 };

 class AlarmClock : public Clock {
     Time a;
     int on;
   public:
     AlarmClock() {
         on = 0;
         cout << "A$";
         show();
     }
     AlarmClock(const Time& c, const Time& a) : Clock(c) {
         on = 0;
         this->a = a;
         cout << "A*" << endl;
         show();
         cout << endl;
     }
     AlarmClock(const AlarmClock& ac) : Clock(ac) {
         on = ac.on;
         a  = ac.a;
         cout << "A#";
         show();
     }
     ~AlarmClock() {
         cout << "A%" << endl;
         show();
         cout << endl;
     }
     void show() const {
         Clock::show();
         cout << "A:";
         a.show();
     }
     void flip() { on = !on; }
     void tick() {
         Clock::tick();
         if (on == 1 && a == time())
             cout << "brring..." << endl;
         else if (on == 1)
             cout << "zzzzzz..." << endl;
     }
 };

 int main() {
     AlarmClock a[2] = {AlarmClock(Time(11,59),Time(12,0)),
                        AlarmClock(Time( 6,59),Time( 7,0))};

     cout << "---------" << endl;
     a[1].flip();
     a[0].tick();
     a[1].tick();
     a[0].flip();
     a[0].tick();
     a[1].tick();
     cout << "---------" << endl;

     return 0;
 }

Show your output here:











































































   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site