Another Example: A TimeType Class

We introduce here a TimeType abstract data type for the time of a day.

class TimeType

{
public:
      void Set(int hours, int minutes, int seconds);
            // Preconditions:
            // 0 <= hours <= 23 && 0 <= minutes <= 59
            // && 0 <= seconds <= 59
            // Postconditions:
            // Time is set according to the incoming parameters

      void Increment( );
            //Postcondition:
            // Time has been advanced by one second,
            // with 23:59:59 wrapping arround to 0:0:0

      void Write ( ) const;
            //Postcondition:
            // Time has been output in the form HH:MM:SS

      bool Equal( TimeType otherTime) const;
           // Postcondition:
           // function value == true, if this time equals otherTime
            //                              == false, otherwise

      bool LessThan( TimeType otherTime) const;
            //Precondition
            // This time and otherTime represent times in the same day
            // Postcondition:
            // function value == true, if this time is earlier in the day than otherTime
            // == false, otherwise

      TimeType(int initHrs, int initMins, int initSecs);
            // Constructor
            // Precondition: initHrs, initMins, initSecs have values in the correct range
            // Postcondition: hrs, mins, secs are set to the input values

      TimeType( ); // Constructor
           // Postcondition:
           // Class object is constructed && Time is 0:0:0

private:
      int hrs;
      int mins
      int secs;
};

//*******************************************************************
TimeType::TimeType(int initHrs, int initMins, int initSecs)
{
      hrs = initHrs;
      mins = initMins;
      secs = initSecs;
}

//*******************************************************************
TimeType::TimeType( )
{
      hrs = 0;
      mins = 0;
      secs = 0;
}

//*******************************************************************
void TimeType::Set(int hours, int minutes, int seconds )
{
      hrs = hours;
      mins = minutes;
      secs = seconds;
}

//*******************************************************************
void TimeType::Increment( )
{
      secs++;
      if (secs > 59)
      {
           secs = 0;
           mins++;
           if (mins > 59)
           {
                mins = 0;
                hrs++;
                if (hrs > 23)
                hrs = 0;
           }
      }
}

//******************************************************************
void TimeType::Write( ) const
{
      if (hrs < 10)
           cout << '0';
      cout << hrs << ':';
      if (mins < 10)
           cout << '0';
      cout << mins << ':';
      if (secs < 10)
           cout << '0';
      cout << secs;
}

//******************************************************************
Bool TimeType::Equal(TimeType otherTime) const
{
      return (hrs == otherTime.hrs && mins == otherTime.mins &&
           secs == otherTime.secs);
}

//******************************************************************
Bool TimeType::LessThan(TimeType otherTime) const
{
      return (hrs < otherTime.hrs ||
           hrs == otherTime.hrs && mins < otherTime.mins ||
           hrs == otherTime.hrs && mins == otherTime.mins &&
           secs < otherTime.secs);
}


We have declared certain member functions as constant using the keyword const. This guarantees that the function cannot alter the state of the class.

The following code fragment illustrates how the above TimeType class can be used:

TimeType presentTime, appointment;
int Hrs, Mins, Secs;
cout << "Enter the present time in hours, minutes and seconds: ";
cin >> Hrs >> Mins >> Secs;
presentTime.set(Hrs,Mins,Secs);
cout << "Enter your appointment time in hours, minutes and seconds: ";
cin >> Hrs >> Mins >> Secs;
appointment.set(Hrs,Mins,Secs);
cout << "The appointment time is";
appointment.Write( );
. . .