Source of time2.cpp


  1: // Filename: TIME2.CPP
  2: // Purpose:  Implementation file corresponding to TIME1.H.
  3: //           This version adds two constructors to the version in TIME1.CPP.

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

  8: #include "TIME2.H"


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


 17: //******************************************************************
 18: Time::Time()
 19: // Default constructor
 20: // Pre:  none
 21: // Post: Class object is constructed and the time is 00:00:00.
 22: //       That is, hours, minutes and seconds all have value 0.
 23: {
 24:     hours = 0;
 25:     minutes = 0;
 26:     seconds = 0;
 27: }


 30: //******************************************************************
 31: Time::Time(/* in */ int hoursInitial,
 32:            /* in */ int minutesInitial,
 33:            /* in */ int secondsInitial)
 34: // Constructor
 35: // Pre:      0 <= hoursInitial   <= 23
 36: //           0 <= minutesInitial <= 59
 37: //       and 0 <= secondsInitial <= 59
 38: // Post: Class object is constructed.
 39: //       The time is set according to the incoming parameters.
 40: {
 41:     hours = hoursInitial;
 42:     minutes = minutesInitial;
 43:     seconds = secondsInitial;
 44: }


 47: //******************************************************************
 48: void Time::Set(/* in */ int hoursNew,
 49:                /* in */ int minutesNew,
 50:                /* in */ int secondsNew)
 51: // Pre:  0 <= hoursNew <= 23
 52: //       0 <= minutesNew <= 59
 53: //       0 <= secondsNew <= 59
 54: // Post: Time is set according to the incoming parameters.
 55: {
 56:     hours = hoursNew;
 57:     minutes = minutesNew;
 58:     seconds = secondsNew;
 59: }



 63: //******************************************************************
 64: void Time::Increment()
 65: // Pre:  none
 66: // Post: Time has been advanced by one second, with
 67: //       23:59:59 wrapping around to 00:00:00.
 68: {
 69:     seconds++;
 70:     if (seconds > 59)
 71:     {
 72:         seconds = 0;
 73:         minutes++;
 74:         if (minutes > 59)
 75:         {
 76:             minutes = 0;
 77:             hours++;
 78:             if (hours > 23)
 79:                 hours = 0;
 80:         }
 81:     }
 82: }


 85: //******************************************************************
 86: void Time::Display() const
 87: // Pre:  none
 88: // Post: Time has been output in the form HH:MM:SS.
 89: {
 90:     if (hours < 10)
 91:         cout << '0';
 92:     cout << hours << ':';
 93:     if (minutes < 10)
 94:         cout << '0';
 95:     cout << minutes << ':';
 96:     if (seconds < 10)
 97:         cout << '0';
 98:     cout << seconds;
 99: }