// CS2310 Chapter 11 Excise // Taken from textbook p623 - 6 // // Add new member functions NotEqual, LessOrEqual, GreaterThan, // and GreaterOrEqual to the TimeType class. // //****************************************************************** // IMPLEMENTATION FILE (timetyp2.cpp) // This file implements the TimeType member functions //****************************************************************** #include "timetyp2.h" #include using namespace std; // Private members of class: // int hrs; // int mins; // int secs; //****************************************************************** 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 { hrs = initHrs; mins = initMins; secs = initSecs; } //****************************************************************** TimeType::TimeType() // Default constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 { hrs = 0; mins = 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 { hrs = hours; mins = minutes; secs = seconds; } //****************************************************************** void TimeType::Increment() // Postcondition: // Time has been advanced by one second, with // 23:59:59 wrapping around to 0:0:0 { secs++; if (secs > 59) { secs = 0; mins++; if (mins > 59) { mins = 0; hrs++; if (hrs > 23) hrs = 0; } } } //****************************************************************** void TimeType::Write() const // Postcondition: // Time has been output in the form HH:MM:SS { if (hrs < 10) cout << '0'; cout << hrs << ':'; if (mins < 10) cout << '0'; cout << mins << ':'; if (secs < 10) cout << '0'; cout << secs; } //****************************************************************** bool TimeType::Equal( /* in */ TimeType otherTime ) const // Postcondition: // Function value == true, if this time equals otherTime // == false, otherwise { return (hrs == otherTime.hrs && mins == otherTime.mins && 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 (hrs < otherTime.hrs || hrs == otherTime.hrs && mins < otherTime.mins || hrs == otherTime.hrs && mins == otherTime.mins && secs < otherTime.secs); } bool TimeType::NotEqual( /* in */ TimeType otherTime ) const // Postcondition: // Function value == true, if this time does not // equal otherTime // == false, otherwise { // Write your implementation here. // Hint: consider how to use the return value of the Equal // function } bool TimeType::LessOrEqual( /* 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 // or equal to otherTime // == false, otherwise { // Write your implementation here. // Hint: consider how to use the return value of the Equal // and LessThan functions } bool TimeType::GreaterThan( /* in */ TimeType otherTime ) const // Precondition: // This time and otherTime represent times in the // same day // Postcondition: // Function value == true, if this time is later // in the day than otherTime // == false, otherwise { // Write your implementation here. // Hint: consider how to use the return value of the LessOrEqual // function } bool TimeType::GreaterOrEqual( /* in */ TimeType otherTime ) const // Precondition: // This time and otherTime represent times in the // same day // Postcondition: // Function value == true, if this time is later // in the day than otherTime // or equal to otherTime // == false, otherwise { // Write your implementation here. // Hint: consider how to use the return value of the Equal // and GreaterThan functions }