//-------------- digitime.cpp -------------- #include // for cout, cin #include "timer.h" // for Display and Timer declarations using namespace std; //-------------- Definition of member functions for class Display Display::Display(int lim) : LIMIT(lim) // Initialize a new Display object. { value = 0; } void Display::Increment() // Add 1 to value. If incrementing makes value // equal to limit, reset value to zero. { value++; if (value == LIMIT) value = 0; } bool Display::SetValue(int val) // Set the value. If the argument is negative, we make it positive. // To make sure value is within the right range, we set the value to // its remainder upon division by limit. { if (val < 0 || val >= LIMIT) return false; value = val; return true; } int Display::GetValue() const // Return the current value. { return value; } void Display::Show() const // Show the value of a Display. { if (value < 10) // Pad with a leading zero, if needed, cout << '0'; cout << value; // and in any case, display the value. } int Display::GetLimit() const // Return the limit for this display { return LIMIT; } //-------------- Definition of member functions for class Timer Timer::Timer() : hours(24), minutes(60) // Initialize a new Timer object, // setting hours limit to 24 and minutes limit to 60. { // All the work is done by the two constructor calls in the header. } Timer::Timer(int h, int m) : hours(24), minutes(60) { if (Set(h,m) == false) Set(0,0); } void Timer::Increment() // Add 1 minute to timer. { minutes.Increment(); if (minutes.GetValue() == 0) // We've turned the minute counter over, hours.Increment(); // so we have to increment the hours counter. } bool Timer::Set(int h, int m) // Set hours and minutes from the keyboard. { if (h < 0 || h >= hours.GetLimit()) return false; if (m < 0 || m >= minutes.GetLimit()) return false; hours.SetValue(h); minutes.SetValue(m); return true; } void Timer::Show() const // Show the current timer's settings. { hours.Show(); cout << ':'; minutes.Show(); } Timer Timer::Add(const Timer& t) const { int h = hours.GetValue() + t.hours.GetValue(); int m = minutes.GetValue() + t.minutes.GetValue(); if (m >= 60) { m = m - 60; // subtract 60 minutes h++; // add it to hours } if (h >= 24) { h = h - 24; } return Timer(h,m); // build object and return }