这个程序是在不明白应该怎样编,希望大神指导!
定义一个时间类,包括小时、分、秒三个数据成员,自定义构造函数和析构函数,并具有设置时间函数SetTime()、返回时间函数GetTime(),以及显示当前时间的成员函数ShowTime()。此外,还需要对=、+=、++、+、==、!=、>=、<、>这些运算符进行重载。
//重载输入输出练习 #include <iostream> #include <iomanip> using namespace std; class Time { public: Time(); Time(int x, int y):i(x), j(y){} ~Time(); friend ostream& operator<<(ostream& sc, Time& c); friend istream& operator>>(istream& sr, Time& c); Time operator+(Time& c) { Time t; t.i = i + c.i; if(j + c.j > 59) { ++t.i; t.j = (j + c.j) - 60; } return t; } Time operator++()//前++ { if(j > 59) { j -= 60; ++i; } else { ++j; } return *this; } // Time operator++(int) private: int i; int j; }; Time::Time() { i = 0; j = 0; } Time::~Time() { } ostream& operator<<(ostream& sc, Time& c) { sc << setw(2) << setfill('0') << c.i << ":" << setw(2) << setfill('0') << c.j << endl; return sc; } istream& operator>>(istream& sr, Time& c) { sr >> c.i >> c.j; return sr; } int main(void) { Time t1, t2; cin >> t1 >> t2; Time t3 = t1 + t2; cout << t1 << t2 << endl << t3; for(int i = 0; i < 60; ++i) { cout << t3++; } return 0; }