Source of time.cpp


  1: /** @file time.cpp
  2: Implementation file corresponding to time.h.
  3: This version adds three overloaded operators and default
  4: parameters in order to combine constructor definitions.
  5: @author Scobey:Porter:A00123456:CSC34101
  6: @date October 5, 2010
  7: @version 2.0
  8: */

 10: #include <iostream>
 11: using namespace std;

 13: #include "time.h"

 15: //Friends of the Time class
 16: //******************************************************************
 17: istream& operator>>
 18:     (
 19:     istream& inputStream, //inout
 20:     Time& t               //out
 21:     )
 22: {
 23:     char junkChar;
 24:     inputStream >> t.hours   >> junkChar
 25:         >> t.minutes >> junkChar
 26:         >> t.seconds;
 27:     return inputStream;
 28: }


 31: //******************************************************************
 32: ostream& operator<<
 33:     (
 34:     ostream& outputStream, //inout
 35:     const Time& t          //in
 36:     )
 37: {
 38:     if (t.hours < 10) outputStream << '0';
 39:     outputStream << t.hours << ':';
 40:     if (t.minutes < 10) outputStream << '0';
 41:     outputStream << t.minutes << ':';
 42:     if (t.seconds < 10) outputStream << '0';
 43:     outputStream << t.seconds;
 44:     return outputStream;
 45: }


 48: /*
 49: Private data members of the Time class:
 50: int hours;
 51: int minutes;
 52: int seconds;
 53: */

 55: //******************************************************************
 56: Time::Time
 57:     (
 58:     int hoursInitial,   //in
 59:     int minutesInitial, //in
 60:     int secondsInitial  //in
 61:     )
 62: {
 63:     hours = hoursInitial;
 64:     minutes = minutesInitial;
 65:     seconds = secondsInitial;
 66: }


 69: //******************************************************************
 70: void Time::set
 71:     (
 72:     int hoursNew,   //in
 73:     int minutesNew, //in
 74:     int secondsNew  //in
 75:     )
 76: {
 77:     hours = hoursNew;
 78:     minutes = minutesNew;
 79:     seconds = secondsNew;
 80: }


 83: //******************************************************************
 84: int Time::getHours()   const { return hours; }
 85: int Time::getMinutes() const { return minutes; }
 86: int Time::getSeconds() const { return seconds; }


 89: //******************************************************************
 90: void Time::increment()
 91: {
 92:     seconds++;
 93:     if (seconds > 59)
 94:     {
 95:         seconds = 0;
 96:         minutes++;
 97:         if (minutes > 59)
 98:         {
 99:             minutes = 0;
100:             hours++;
101:             if (hours > 23)
102:                 hours = 0;
103:         }
104:     }
105: }


108: //******************************************************************
109: Time Time::operator+
110:     (
111:     const Time& otherTime //in
112:     ) const
113: {
114:     Time t;
115:     int secs = seconds + otherTime.seconds;
116:     t.seconds = secs%60;
117:     int mins = minutes + otherTime.minutes + secs/60;
118:     t.minutes = mins%60;
119:     int hrs = hours + otherTime.hours + mins/60;
120:     t.hours = hrs%24;
121:     return t;
122: }



126: //******************************************************************
127: bool Time::operator==
128:     (
129:     const Time& otherTime //in
130:     ) const
131: {
132:     return (hours == otherTime.hours      &&
133:         minutes == otherTime.minutes  &&
134:         seconds == otherTime.seconds);
135: }