|
//chap12p2.cpp //The complete program listing of the program that defines //and uses the class clockType #include <iostream> using namespace std; class clockType { private: int hr; int min; int sec; public: clockType(void); clockType(int hours, int minutes, int seconds); void setTime(int hours, int minutes, int seconds); void getTime(int &hours, int &minutes, int &seconds) const; void printTime(void) const; void incrementSeconds(void); void incrementMinutes(void); void incrementHours(void); bool equalTime(const clockType &t) const; bool operator == (const clockType &t) const; bool operator != (const clockType &t) const; bool operator < (const clockType &t) const; friend ostream & operator<<(ostream &tom, const clockType &c); friend bool operator > (const clockType &c1, const clockType &c2); }; void test_operator_notequal(void); void test_operator_lessthan(void); void main(void) { clockType myClock1(1,2,5); cout << "myClock1="; cout << myClock1; cout << endl; clockType myClock2(1,2,3); cout << "myClock2="; cout << myClock2; cout << endl; if (myClock1 > myClock1) cout << "myClock1 is < myClock1\n"; else cout << "myClock1 is NOT < myClock1\n"; if (myClock1 > myClock2) cout << "myClock1 is < myClock2\n"; else cout << "myClock1 is NOT < myClock2\n"; return; //test_operator_notequal(); return; test_operator_lessthan(); return; clockType myClock(1,2,3); //testing 2nd constructor clockType yourClock(4,5,6); if (myClock == yourClock) cout << "myClock is equal to yourClock\n"; else cout << "myClock is NOT equal to yourClock\n"; myClock = yourClock; cout << "After myClock = yourClock;\n"; if (myClock == yourClock) cout << "myClock is equal to yourClock\n"; else cout << "myClock is NOT equal to yourClock\n"; //Set the time of myClock myClock.setTime(5, 4, 30); //Line 1 cout << "Line 2: myClock: "; //Line 2 myClock.printTime(); //print the time of myClock Line 3 cout << endl; //Line 4 cout << "Line 5: yourClock: "; //Line 5 yourClock.printTime(); //print the time of yourClock Line 6 cout << endl; //Line 7 //Set the time of yourClock yourClock.setTime(5, 45, 16); //Line 8 cout << "Line 9: After setting, yourClock: "; //Line 9 yourClock.printTime(); //print the time of yourClock Line 10 cout << endl; //Line 11 //Compare myClock and yourClock if (myClock.equalTime(yourClock)) //Line 12 cout << "Line 13: Both times are equal." << endl; //Line 13 else //Line 14 cout << "Line 15: The two times are not equal." << endl; //Line 15 int hours; int minutes; int seconds; cout << "Line 16: Enter the hours, minutes, and " << "seconds: "; //Line 16 cin >> hours >> minutes >> seconds; //Line 17 cout << endl; //Line 18 //Set the time of myClock using the value of the //variables hours, minutes, and seconds myClock.setTime(hours, minutes, seconds); //Line 19 cout << "Line 20: New myClock: "; //Line 20 myClock.printTime(); //print the time of myClock Line 21 cout << endl; //Line 22 //Increment the time of myClock by one second myClock.incrementSeconds(); //Line 23 cout << "Line 24: After incrementing myClock by " << "one second, myClock: "; //Line 24 myClock.printTime(); //print the time of myClock Line 25 cout << endl; //Line 26 //Retrieve the hours, minutes, and seconds of the //object myClock myClock.getTime(hours, minutes, seconds); //Line 27 //Output the value of hours, minutes, and seconds cout << "Line 28: hours = " << hours << ", minutes = " << minutes << ", seconds = " << seconds << endl; //Line 28 }//end main ostream &operator<<(ostream &tom, const clockType &c) { if (c.hr < 10) tom << "0"; tom << c.hr << ":"; if (c.min < 10) tom << "0"; tom << c.min << ":"; if (c.sec < 10) tom << "0"; tom << c.sec; return tom; } clockType::clockType(int hours, int minutes, int seconds) { cout << "2nd Constructor is being called\n"; if (hours >= 0 && hours < 24) hr = hours; else hr = 0; if (minutes >= 0 && minutes < 60) min = minutes; else min = 0; if (seconds >= 0 && seconds < 60) sec = seconds; else sec = 0; } clockType::clockType(void) { cout << "default Constructor is being called\n"; hr=min=sec=0; } void clockType::setTime(int hours, int minutes, int seconds) { if (hours >= 0 && hours < 24) //(*this).hr = hours; //this->hr=hours; hr = hours; else hr = 0; if (minutes >= 0 && minutes < 60) min = minutes; else min = 0; if (seconds >= 0 && seconds < 60) sec = seconds; else sec = 0; } void clockType::getTime(int& hours, int& minutes, int& seconds) const { hours = hr; minutes = min; seconds = sec; } void clockType::incrementHours() { hr++; if (hr > 23) hr = 0; } void clockType::incrementMinutes() { min++; if (min > 59) { min = 0; incrementHours(); //increment hours //this->incrementHours(); //increment hours } } void clockType::incrementSeconds() { sec++; if (sec > 59) { sec = 0; incrementMinutes(); //increment minutes } } void clockType::printTime() const { if (hr < 10) cout << "0"; cout << hr << ":"; if (min < 10) cout << "0"; cout << min << ":"; if (sec < 10) cout << "0"; cout << sec; } bool clockType::equalTime(const clockType& otherClock) const { return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec); } bool clockType::operator == (const clockType& otherClock) const { return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec); } bool clockType::operator != (const clockType& otherClock) const { return !(*this == otherClock); //if (otherClock == *this) // return false; //else // return true; //return (hr != otherClock.hr // || min != otherClock.min // || sec != otherClock.sec); } void test_operator_notequal(void) { clockType t1(1,2,3), t2(4,5,6); t1.printTime(); cout << endl; t2.printTime(); cout << endl; if(t1 != t2) cout << "Times are NOT equal\n"; else cout << "Times are equal\n"; t1 = t2; cout << "After t1 = t2;\n"; t1.printTime(); cout << endl; t2.printTime(); cout << endl; if(t1.operator != (t2)) cout << "Times are NOT equal\n"; else cout << "Times are equal\n"; if(t1 != t2) cout << "Times are NOT equal\n"; else cout << "Times are equal\n"; } bool clockType::operator < (const clockType& otherClock) const { /* if h1 < h2 return true if h1 > h2 return false //that means h1 == h2 if m1 < m2 return true if m1 > m2 return false //that means m1 == m2 and h1==h2 if s1 < s2 return true return false */ int h1, m1, s1, h2, m2, s2; h1 = this->hr; m1 = this->min; s1 = this->sec; h2 = otherClock.hr; m2 = otherClock.min; s2 = otherClock.sec; if (h1 < h2) return true; if (h1 > h2) return false; ////that means h1 == h2 if (m1 < m2) return true; if (m1 > m2) return false; ////that means m1 == m2 and h1==h2 if (s1 < s2) return true; return false; } bool operator > (const clockType &c1, const clockType &c2) { int h1, m1, s1, h2, m2, s2; h1 = c1.hr; m1 = c1.min; s1 = c1.sec; h2 = c2.hr; m2 = c2.min; s2 = c2.sec; if (h1 > h2) return true; if (h1 < h2) return false; ////that means h1 == h2 if (m1 > m2) return true; if (m1 < m2) return false; ////that means m1 == m2 and h1==h2 if (s1 > s2) return true; return false; } void test_operator_lessthan(void) { clockType t1(1,2,3), t2(4,5,6); t1.printTime(); cout << endl; t2.printTime(); cout << endl; if(t1 < t2) cout << "t1 is less than t2\n"; else cout << "t1 is NOT less than t2\n"; t1 = t2; cout << "After t1 = t2;\n"; t1.printTime(); cout << endl; t2.printTime(); cout << endl; if(t1 < t2) cout << "t1 is less than t2\n"; else cout << "t1 is NOT less than t2\n"; t2.setTime(4,5,5); cout << "After t2.setTime(4,5,5);\n"; t1.printTime(); cout << endl; t2.printTime(); cout << endl; if(t1 < t2) cout << "t1 is less than t2\n"; else cout << "t1 is NOT less than t2\n"; t2.setTime(4,4,5); cout << "After t2.setTime(4,4,5);\n"; t1.printTime(); cout << endl; t2.printTime(); cout << endl; if(t1 < t2) cout << "t1 is less than t2\n"; else cout << "t1 is NOT less than t2\n"; t2.setTime(3,5,5); cout << "After t2.setTime(3,5,5);\n"; t1.printTime(); cout << endl; t2.printTime(); cout << endl; if(t1 < t2) cout << "t1 is less than t2\n"; else cout << "t1 is NOT less than t2\n"; } |