// Filename: Timer.cxx // Timer class: provides a countdown timer with minutes and seconds // Implementation #include #include "Timer.h" using namespace std; const int secInMin = 60; // number of seconds in minute // The constructor Timer::Timer(int minutes, int seconds) { set(minutes, seconds); } // sets the minutes and seconds of timer void Timer::set(int minutes, int seconds) { theSeconds = minutes * secInMin + seconds; if (theSeconds < 0) { cerr << "Initialization must be non-negative. Timer at zero.\n"; theSeconds = 0; } } // prints the minutes and seconds remaining void Timer::print(ostream &os) const { char fillCh; // starting fill character os << theSeconds / secInMin << ":"; os.width(2); fillCh = os.fill('0'); os << theSeconds % secInMin; os.fill(fillCh); } // subtracts minutes and seconds from the timer // maintains timer at no less than zero void Timer::subtract(int minutes, int seconds) { theSeconds -= (minutes * secInMin) + seconds; if (theSeconds < 0) { theSeconds = 0; } } // returns whether timer has run out of time // returns 1 if out of time, 0 if there is time remaining int Timer::outOfTime( ) const { return theSeconds == 0; } // prints t to ostream os ostream & operator << (ostream & os, const Timer & t) { t.print(os); return os; }