Source of time1.cpp


  1: // Filename: TIME1.CPP
  2: // Purpose:  Implementation file correponding to TIME1.H.

  4: #include <iostream>
  5: using namespace std;


  8: #include "TIME1.H"


 11: // Private data members of the Time class:
 12: // int hours;
 13: // int minutes;
 14: // int seconds;


 17: //******************************************************************
 18: void Time::Set(/* in */ int hoursNew,
 19:                /* in */ int minutesNew,
 20:                /* in */ int secondsNew)
 21: // Pre:  0 <= hoursNew   <= 23
 22: //       0 <= minutesNew <= 59
 23: //       0 <= secondsNew <= 59
 24: // Post: Time is set according to the incoming parameters.
 25: // Note that this function MUST have been called prior to
 26: // any call to *any* of the other member functions.
 27: {
 28:     hours = hoursNew;
 29:     minutes = minutesNew;
 30:     seconds = secondsNew;
 31: }



 35: //******************************************************************
 36: void Time::Increment()
 37: // Pre:  The Set function has been called at least once.
 38: // Post: Time has been advanced by one second, with
 39: //       23:59:59 wrapping around to 00:00:00.
 40: {
 41:     seconds++;
 42:     if (seconds > 59)
 43:     {
 44:         seconds = 0;
 45:         minutes++;
 46:         if (minutes > 59)
 47:         {
 48:             minutes = 0;
 49:             hours++;
 50:             if (hours > 23)
 51:                 hours = 0;
 52:         }
 53:     }
 54: }



 58: //******************************************************************
 59: void Time::Display() const
 60: // Pre:  The Set function has been called at least once.
 61: // Post: Time has been output in the form HH:MM:SS.
 62: {
 63:     if (hours < 10)
 64:         cout << '0';
 65:     cout << hours << ':';
 66:     if (minutes < 10)
 67:         cout << '0';
 68:     cout << minutes << ':';
 69:     if (seconds < 10)
 70:         cout << '0';
 71:     cout << seconds;
 72: }