Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition,
© 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3
Figure 4.2 OOP Interface for Time |
/*-- Time.h --------------------------------------------------------------- This header file defines the data type Time for processing time. Basic operations are: set: To set the time display: To display the time -------------------------------------------------------------------------*/ #include <iostream> class Time { public: /******** Function Members ********/ void set(unsigned hours, unsigned minutes, char am_pm); /*---------------------------------------------------------------------- Set the data members of a Time object to specified values. Preconditions: 1 <= hours <= 12, 0 <= minutes <= 59, and am_pm is either 'A' or 'P'. Postcondition: Data members myHours, myMinutes, and myAMorPM are set to hours, minutes, and am_pm, respectively, and myMilTime to the equivalent military time ----------------------------------------------------------------------*/ void display(ostream & out) const; /*---------------------------------------------------------------------- Display time in standard and military format using output stream out. Precondition: The ostream out is open. Postcondition: The time represented by this Time object has been inserted into ostream out. ----------------------------------------------------------------------*/ private: /********** Data Members **********/ unsigned myHours, myMinutes; char myAMorPM; // 'A' or 'P' unsigned myMilTime; // military time equivalent }; // end of class declaration |
Figure 4.3 OOP Implementation of Time |
/*-- Time.cpp------------------------------------------------------------ Definitions of the function members of the Time class declared in Time.h and definitions of utility functions that convert between military and standard time. -------------------------------------------------------------------------*/ #include <iostream> using namespace std; #include "Time.h" /*** Utility Functions -- Prototypes ***/ int toMilitary(unsigned hours, unsigned minutes, char am_pm); //----- Definition of set function ----- void Time::set(unsigned hours, unsigned minutes, char am_pm) { // Check class invariant if (hours >= 1 && hours <= 12 && minutes >= 0 && minutes <= 59 && (am_pm == 'A' || am_pm == 'P')) { myHours = hours; myMinutes = minutes; myAMorPM = am_pm; myMilTime = toMilitary(hours, minutes, am_pm); } else cerr << "*** Can't set time with these values ***\n"; // Object's data members remain unchanged } //----- Definition of display function ----- void Time::display(ostream & out) const { out << myHours << ':' << (myMinutes < 10 ? "0" : "") << myMinutes << ' ' << myAMorPM << ".M. (" << myMilTime << " mil. time)"; } /*** Utility Functions -- Definitions ***/ int toMilitary(unsigned hours, unsigned minutes, char am_pm) /*------------------------------------------------------------------------- Convert standard time to military time. Precondition: hours, minutes, am_pm satisfy the class invariant. Postcondition: Military time equivalent is returned. -------------------------------------------------------------------------*/ { if (hours == 12) hours = 0; return hours * 100 + minutes + (am_pm == 'P' ? 1200 : 0); } |
Figure 4.4 Test Driver for Time Class |
//--- Test driver for class Time
#include <iostream>
using namespace std;
#include "Time.h"
int main()
{
Time mealTime;
mealTime.set(5, 30, 'P');
cout << "We'll be eating at ";
mealTime.display(cout);
cout << endl;
cout << "\nNow trying to set time with illegal hours (13)" << endl;
mealTime.set(13, 0, 'A');
cout << "Now trying to set time with illegal minutes (60)" << endl;
mealTime.set(5, 60, 'A');
cout << "Now trying to set time with illegal AM/PM ('X')" << endl;
mealTime.set(5, 30, 'X');
}
|
Figure 4.5A Expanded Time.h |
/*-- Time.h --------------------------------------------------------------- This header file defines the data type Time for processing time. Basic operations are: Default constructor -- Initializes object to 12:00 AM Explicit-value constructor read: To read a time display: To display the time >>, <<: Input and Output operators accessors: getHours, getMinutes, getAMPM, getMilTime set: To set the time advance: To advance the time by a certain amount relops: <, >, ==, <=, >=, != -------------------------------------------------------------------------*/ #include <iostream> using namespace std; #ifndef TIME #define TIME class Time { public: /******** Function Members ********/ /***** Class constructors *****/ Time(); /*---------------------------------------------------------------------- Construct a class object (default). Precondition: None. Postcondition: Time object is initialized to 12:00 A.M.; that is, myHours, myMinutes, and myAMorPM are initialized to 12, 0, 'A', respectively, and myMilTime to 0. -----------------------------------------------------------------------*/ Time(unsigned initHours, unsigned initMinutes, char initAMPM); /*---------------------------------------------------------------------- Construct a class object (explicit values). Precondition: initHours, initMinutes, and initAMPM are initial values for the data members; they must preserve the class invariant. Postcondition: Data members myHours, myMinutes, and myAMorPM have been initialized to initHours, initMinutes, and initAMPM, respectively, and myMilTime to the corresponding military time. -----------------------------------------------------------------------*/ /***** Accessors *****/ unsigned getHours() const; /*---------------------------------------------------------------------- Retrieve the value stored in the myHours data member. Precondition: None. Postcondition: Value stored in myHours is returned. -----------------------------------------------------------------------*/ unsigned getMinutes() const; /*---------------------------------------------------------------------- Retrieve the value stored in the myMinutes data member. Precondition: None. Postcondition: Value stored in myMinutes is returned. -----------------------------------------------------------------------*/ unsigned getAMPM() const; /*---------------------------------------------------------------------- Retrieve the value stored in the myAMorPM data member. Precondition: None. Postcondition: Value stored in myAMorPM is returned. -----------------------------------------------------------------------*/ unsigned getMilTime() const; /*---------------------------------------------------------------------- Retrieve the value stored in the myMilTime data member. Precondition: None. Postcondition: Value stored in myMilTime is returned. -----------------------------------------------------------------------*/ /***** Input/Output *****/ void display(ostream & out) const; /*---------------------------------------------------------------------- Display time in standard and military format using output stream out. Precondition: The ostream out is open. Postcondition: The time represented by this Time object has been inserted into ostream out. ----------------------------------------------------------------------*/ void read(istream & in); /*---------------------------------------------------------------------- Read a time value from input stream in. Precondition: The istream in is open; input from in has the form hh:mm xM; values hh, mm, and X satisfy the class invariant. Postcondition: Input values have been removed from in and stored in the data members. ----------------------------------------------------------------------*/ /***** Set operation *****/ void set(unsigned hours, unsigned minutes, char am_pm); /*---------------------------------------------------------------------- Set the data members of a Time object to specified values. Preconditions: 1 <= hours <= 12, 0 <= minutes <= 59, and am_pm is either 'A' or 'P'. Postconditon: Data members myHours, myMinutes, and myAMorPM are set to hours, minutes, and am_pm, respectively, and myMilTime to the equivalent military time ----------------------------------------------------------------------*/ /***** Increment operation *****/ void advance(unsigned hours, unsigned minutes); /*---------------------------------------------------------------------- Increment a Time object by a specified value. Precondition: hours is the number of hours to add and minutes is the number of minutes to add. Postcondition: The time represented by this Time object had been incremented by this number of hour and minutes. ----------------------------------------------------------------------*/ private: /********** Data Members **********/ unsigned myHours, myMinutes; char myAMorPM; // 'A' or 'P' unsigned myMilTime; // military time equivalent }; // end of class declaration //----- << and >> operators ostream & operator<<(ostream & out, const Time & t); /*------------------------------------------------------------------------ Overloaded ouput operator Precondition: The ostream out is open. Postcondition: The time represented by this Time object has been inserted into ostream out (via display()); reference to out is returned. ------------------------------------------------------------------------*/ istream & operator>>(istream & in, Time & t); /*------------------------------------------------------------------------ Overloaded input operator Precondition: The istream in is open; input from in has the form hh:mm xM; values hh, mm, and X satisfy the class invariant. Postcondition: Values have been extracted from in (via read()) and stored in this Time object's data members; reference to in is returned. --------------------------------------------------------------------------*/ /***** Relational operators *****/ bool operator<(const Time & t1, const Time & t2); /*---------------------------------------------------------------------- Determine if one Time object is less than (i.e., earlier than) another Time object. Precondition: None. Postcondition: true is returned if t1 is less than t2 and false otherwise. ---------------------------------------------------------------------*/ bool operator>(const Time & t1, const Time & t2); /*---------------------------------------------------------------------- Determine if one Time object is greater than (i.e., later than) another Time object. Precondition: None. Postcondition: true is returned if t1 is greater than t2 and false otherwise. ----------------------------------------------------------------------*/ bool operator==(const Time & t1, const Time & t2); /*---------------------------------------------------------------------- Determine if one Time object is equal to another Time object. Precondition: None. Postcondition: true is returned if t1 is equal to t2 and false otherwise. ----------------------------------------------------------------------*/ bool operator<=(const Time & t1, const Time & t2); /*---------------------------------------------------------------------- Determine if one Time object is less than or equal to (i.e., earlier than or the same as) another Time object. Precondition: None. Postcondition: true is returned if t1 is less than or equal to t2 and false otherwise. ----------------------------------------------------------------------*/ bool operator>=(const Time & t1, const Time & t2); /*---------------------------------------------------------------------- Determine if one Time object is greater than or equal to (i.e., later than or the same as) another Time object. Precondition: None. Postcondition: true is returned if t1 is greater than or equal to t2 and false otherwise. ----------------------------------------------------------------------*/ //----- Definition of operator!=() ----- bool operator!=(const Time & t1, const Time & t2); /*---------------------------------------------------------------------- Determine if one Time object is not equal to another Time object. Precondition: None. Postcondition: true is returned if t1 is not equal to t2 and false otherwise. ----------------------------------------------------------------------*/ #endif |
Figure 4.5B Expanded Time.cpp |
/*-- Time.cpp------------------------------------------------------------ This file implements the Time member functions. -------------------------------------------------------------------------*/ #include <iostream> using namespace std; #include "Time.h" /*** Utility Functions -- Prototypes ***/ int toMilitary(unsigned hours, unsigned minutes, char am_pm); void toStandard(unsigned military, unsigned & hours, unsigned & minutes, char & AMPM); //----- Definition of default constructor Time::Time() : myHours(12), myMinutes(0), myAMorPM('A'), myMilTime(0) { } //----- Definition of explicit-value constructor ----- Time::Time(unsigned initHours, unsigned initMinutes, char initAMPM) { // Check class invariant if (initHours >= 1 && initHours <= 12 && initMinutes >= 0 && initMinutes <= 59 && (initAMPM == 'A' || initAMPM == 'P')) { myHours = initHours; myMinutes = initMinutes; myAMorPM = initAMPM; myMilTime = toMilitary(initHours, initMinutes, initAMPM); } else cerr << "*** Invalid initial values ***\n"; } //----- Definition of getMinutes() unsigned Time::getMinutes() const { return myMinutes; } //----- Definition of getHours() unsigned Time::getHours() const { return myHours; } //----- Definition of getAMPM() unsigned Time::getAMPM() const { return myAMorPM; } //----- Definition of getMilTime() unsigned Time::getMilTime() const { return myMilTime; } //----- Definition of display function ----- void Time::display(ostream & out) const { out << myHours << ':' << (myMinutes < 10 ? "0" : "") << myMinutes << ' ' << myAMorPM << ".M. (" << myMilTime << " mil. time)"; } //----- Definition of read function ----- void Time::read(istream & in) { unsigned hours, // Local variables to hold input values from in so minutes; // they can be checked against the class invariant char am_pm, // before putting them in the data members ch; // To gobble up : and the 'M' in input in >> hours >> ch >> minutes >> am_pm >> ch; // Check the class invariant if (hours >= 1 && hours <= 12 && minutes >= 0 && minutes <= 59 && (am_pm == 'A' || am_pm == 'P')) { myHours = hours; myMinutes = minutes; myAMorPM = am_pm; myMilTime = toMilitary(hours, minutes, am_pm); } else cerr << "*** Invalid input for Time object ***\n"; } //----- Definition of operator<<() ostream & operator<<(ostream & out, const Time & t) { t.display(out); return out; } //----- Definition of operator>>() istream & operator>>(istream & in, Time & t) { t.read(in); return in; } //----- Definition of set function ----- void Time::set(unsigned hours, unsigned minutes, char am_pm) { // Check class invariant if (hours >= 1 && hours <= 12 && minutes >= 0 && minutes <= 59 && (am_pm == 'A' || am_pm == 'P')) { myHours = hours; myMinutes = minutes; myAMorPM = am_pm; myMilTime = toMilitary(hours, minutes, am_pm); } else cerr << "*** Can't set time with these values ***\n"; // Object's data members remain unchanged } //----- Definition of advance function----- void Time::advance(unsigned hours, unsigned minutes) { // Increment the myMilTime member myMilTime += 100 * hours + minutes; // Adjust to proper format unsigned milHours = myMilTime / 100, milMins = myMilTime % 100; milHours += milMins / 60; milMins %= 60; milHours %= 24; myMilTime = 100 * milHours + milMins; // Now set the standard time data members toStandard(myMilTime, myHours, myMinutes, myAMorPM); } /***** Relational operators *****/ bool operator<(const Time & t1, const Time & t2) { return t1.getMilTime() < t2.getMilTime(); } bool operator>(const Time & t1, const Time & t2) { return t1.getMilTime() > t2.getMilTime(); } bool operator==(const Time & t1, const Time & t2) { return t1.getMilTime() == t2.getMilTime(); } bool operator<=(const Time & t1, const Time & t2) { return t1.getMilTime() <= t2.getMilTime(); } bool operator>=(const Time & t1, const Time & t2) { return t1.getMilTime() >= t2.getMilTime(); } //----- Definition of operator!=() ----- bool operator!=(const Time & t1, const Time & t2) { return t1.getMilTime() != t2.getMilTime(); } /*** Utility Functions -- Definitions ***/ int toMilitary(unsigned hours, unsigned minutes, char am_pm) /*------------------------------------------------------------------------- Convert standard time to military time. Precondition: hours, minutes, am_pm satisfy the class invariant. Postcondition: Military time equivalent is returned. -------------------------------------------------------------------------*/ { if (hours == 12) hours = 0; return hours * 100 + minutes + (am_pm == 'P' ? 1200 : 0); } void toStandard(unsigned military, unsigned & hours, unsigned & minutes, char & AMPM) /*------------------------------------------------------------------------- Convert military time to standard time. Receive: military, a time in military format Return: hours, minutes, AMPM -- equivalent standard time -------------------------------------------------------------------------*/ { hours = (military / 100) % 12; if (hours == 0) hours = 12; minutes = military % 100; AMPM = (military / 100) < 12 ? 'A' : 'P'; } |
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|
Figure |
|