Source of time3.cpp


  1: // Filename: TIME3.CPP
  2: // Purpose:  Implementation file corresponding to TIME3.H. This version
  3: //           adds three accessor functions and a friend function.

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

  8: #include "TIME3.H"

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


 16: // The following five "interface functions" are exactly the same
 17: // as those in TIME2.CPP.  For brevity we have omitted the pre-
 18: // and post-conditions, which are also the same. These functions
 19: // are also implemented in exactly the same way as in TIME2.CPP,
 20: // except for the default constructor, which should be compared
 21: // with that in TIME2.CPP.

 23: //******************************************************************
 24: Time::Time() { hours = minutes = seconds = 0; }


 27: //******************************************************************
 28: Time::Time(int hoursInitial, int minutesInitial, int secondsInitial)
 29: {
 30:     hours = hoursInitial;
 31:     minutes = minutesInitial;
 32:     seconds = secondsInitial;
 33: }


 36: //******************************************************************
 37: void Time::Set(int hoursNew, int minutesNew, int secondsNew)
 38: {
 39:     hours = hoursNew;
 40:     minutes = minutesNew;
 41:     seconds = secondsNew;
 42: }


 45: //******************************************************************
 46: void Time::Increment()
 47: {
 48:     seconds++;
 49:     if (seconds > 59)
 50:     {
 51:         seconds = 0;
 52:         minutes++;
 53:         if (minutes > 59)
 54:         {
 55:             minutes = 0;
 56:             hours++;
 57:             if (hours > 23)
 58:                 hours = 0;
 59:         }
 60:     }
 61: }


 64: //******************************************************************
 65: void Time::Display() const
 66: {
 67:     if (hours < 10)
 68:         cout << '0';
 69:     cout << hours << ':';
 70:     if (minutes < 10)
 71:         cout << '0';
 72:     cout << minutes << ':';
 73:     if (seconds < 10)
 74:         cout << '0';
 75:     cout << seconds;
 76: }


 79: // The following three functions are "accessor functions" and provide
 80: // read access to the private data members of the class.

 82: //******************************************************************
 83: int Time::Hours() const
 84: // Pre:  none
 85: // Post: Value of data member "hours" is returned.
 86: {
 87:     return hours;
 88: }


 91: //******************************************************************
 92: int Time::Minutes() const
 93: // Pre:  none
 94: // Post: Value of data member "minutes" is returned.
 95: {
 96:     return minutes;
 97: }


100: //******************************************************************
101: int Time::Seconds() const
102: // Pre:  none
103: // Post: Value of data member "seconds" is returned.
104: {
105:     return seconds;
106: }



110: // The following function is a member function. Note the Time::
111: // preceding the function name and note that as a member function
112: // it has direct access to the private data members.
113: bool Time::EqualTo1(const Time& otherTime) const
114: // Pre:  self and otherTime have been initialized.
115: // Post: Returns true if self and otherTime are the same time
116: //       and false otherwise.
117: {
118:     return (hours   == otherTime.hours    &&
119:             minutes == otherTime.minutes  &&
120:             seconds == otherTime.seconds);
121: }



125: // The following function is *not* a member function (note the
126: // *absence* of Time:: preceding the function name). But note
127: // that it nevertheless has direct access to the private data
128: // members of the class because it is a "friend" of the class.
129: bool EqualTo3(/* in */ const Time& t1,
130:               /* in */ const Time& t2)
131: // Pre:  "t1" and "t2" have been initialized.
132: // Post: Returns true if t1 and t2 are the same time
133: //       and false otherwise.
134: {
135:     return (t1.hours   == t2.hours    &&
136:             t1.minutes == t2.minutes  &&
137:             t1.seconds == t2.seconds);
138: }