自增(二)

#include <iostream>
using namespace std;

class Time {
    public:
        Time(int m=0,int s=0):min(m),sec(s){}
        Time operator++() {
            if (++sec>=60) {
                sec%=60;
                min++;
            }
            return *this;
        }
        Time operator++(int) {
            if (++sec>=60) {
                sec%=60;
                min++;
            }
            return *this;
        }
        friend ostream& operator<<(ostream &,Time);
    private:
        int min;
        int sec;
};

ostream& operator<<(ostream &output,Time t) {
    output<<t.min<<':'<<t.sec<<endl;
    return output;
}

int main()
{
    Time t;
    t++;
    cout<<t;
    ++t;
    cout<<t;
    return 0;
}
posted @ 2019-01-14 10:49  xyee  阅读(165)  评论(0编辑  收藏  举报