// Operator overloading #include using namespace std; class Time { public: friend bool operator<(const Time& t1, const Time& t2); friend istream& operator>>(istream& in, Time& t); Time() : Time(99, 99) {} Time(int h) : Time(h, 0) {} Time(int h, int m) : hours_(h), minutes_(m) {} // bool operator<(const Time & t) const; int hours() const { return hours_; } int minutes() const { return minutes_; } void set_hours(int new_hours) { hours_ = new_hours; } void set_minutes(int new_minutes) { minutes_ = new_minutes; } void set(int new_hours, int new_minutes = 0); private: int hours_; int minutes_; }; //inline bool Time::operator<(const Time& t) const //{ // return (hours_ < t.hours_) || // (hours_ == t.hours_ && minutes_ < t.minutes_); //} // inline bool operator<(const Time& t1, const Time& t2) { return (t1.hours_ < t2.hours_) || (t1.hours_ == t2.hours_ && t1.minutes_ < t2.minutes_); } inline void Time::set(int new_hours, int new_minutes) { hours_ = new_hours; minutes_ = new_minutes; } istream& operator>>(istream& in, Time& t) { // int h; // in >> h; // t.set_hours(h); in >> t.hours_; in.get(); // colon in >> t.minutes_; return in; } ostream& operator<<(ostream& out, const Time& t) { out << t.hours() << ':'; if ( t.minutes() < 10 ) { out << '0'; } out << t.minutes(); return out; } inline void println(const Time& t, ostream& out = cout) { out << t << endl; } void print_with_h(const Time& t, ostream& out) { out << t.hours() << 'h'; if (t.minutes() < 10) { out << '0'; } out << t.minutes(); } // Test driver for Time int main() { Time t1(8, 30); Time t2(12, 45); cout << boolalpha; cout << (t1 < t2) << endl; cout << (t2 < t1) << endl; cout << (t1 < 12) << endl; cout << (8 < t1) << endl; cin >> t1 >> t2; println(t1, cout); println(t2, cout); return 0; }