// CS2310 Chapter 11 Excise // Taken from textbook p624 - 12 // // Reimplement the TimeType class so that the private data // representation is a single variable: // // long secs; // // This variable represents time as the number of seconds since // midnight. Do not change the public interface in any way. The // user's view is still hours, minutes, and seconds, but the // class's view is seconds since midnight. // // Use auxiliary functions, hidden inside the implementation file, // to perform convertions seconds back and forth to hours, // minutes, and seconds, instead of duplicating the algorithms in // in several places. // //****************************************************************** // IMPLEMENTATION FILE (timetyp2.cpp) // This file implements the TimeType member functions //****************************************************************** #include "timetyp2.h" #include using namespace std; // Private members of class: // long secs; // void HrsMinsSecsToSecs(/* in */ int hours, // /* in */ int minutes, // /* in */ int seconds, // /* out */ long& secs); // void SecsToHrsMinsSecs(/* in */ long secs, // /* out */ int& hours, // /* out */ int& minutes, // /* out */ int& seconds); //****************************************************************** void TimeType::HrsMinsSecsToSecs(/* in */ int hours, /* in */ int minutes, /* in */ int seconds, /* out */ long& secs) // Precondition: // 0 <= hours <= 23 && 0 <= minutes <= 59 // && 0 <= seconds <= 59 // Postcondition: // secs = 3600 * hours + 60 * minutes + seconds { // Write your implementation here. } void TimeType::SecsToHrsMinsSecs(/* in */ long secs, /* out */ int& hours, /* out */ int& minutes, /* out */ int& seconds) const // Precondition: // 0 <= secs <= 24 * 3600 - 1 // Postcondition: // hours = secs / 3600 // minutes = secs % 3600 / 60 // seconds = secs % 3600 % 60 { // Write your implementation here. } TimeType::TimeType( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs ) // Constructor // Precondition: // 0 <= initHrs <= 23 && 0 <= initMins <= 59 // && 0 <= initSecs <= 59 // Postcondition: // hrs == initHrs && mins == initMins && secs == initSecs { long seconds; HrsMinsSecsToSecs(initHrs, initMins, initSecs, seconds); secs = seconds; } //****************************************************************** TimeType::TimeType() // Default constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 { secs = 0; } //****************************************************************** void TimeType::Set( /* in */ int hours, /* in */ int minutes, /* in */ int seconds ) // Precondition: // 0 <= hours <= 23 && 0 <= minutes <= 59 // && 0 <= seconds <= 59 // Postcondition: // hrs == hours && mins == minutes && secs == seconds { // Write your implementation here. See how the first // constructor is implemented. } //****************************************************************** void TimeType::Increment() // Postcondition: // Time has been advanced by one second, with // 23:59:59 wrapping around to 0:0:0 { secs = (secs + 1) / (24*3600); } //****************************************************************** void TimeType::Write() const // Postcondition: // Time has been output in the form HH:MM:SS { int hrs, mins, seconds; SecsToHrsMinsSecs(secs, hrs, mins, seconds); if (hrs < 10) cout << '0'; cout << hrs << ':'; if (mins < 10) cout << '0'; cout << mins << ':'; if (seconds < 10) cout << '0'; cout << seconds; } //****************************************************************** bool TimeType::Equal( /* in */ TimeType otherTime ) const // Postcondition: // Function value == true, if this time equals otherTime // == false, otherwise { return (secs == otherTime.secs); } //****************************************************************** bool TimeType::LessThan( /* in */ 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 { return (secs < otherTime.secs); }