#include <iostream>
using namespace std;
class Time
{
public:
    Time(){min=0;sec=0;}
    Time(int m,int s):min(m),sec(s){}
    Time operator++();//重载++i;
    Time operator++(int);//重载i++;
    void display()
    {
        cout<<min<<":"<<sec<<endl;
    }
private:
    int min;//
    int sec;//
};
Time Time::operator++()
{
    if (++sec>=60)
    {
        sec-=60;
        ++min;
    }
    return *this;
}
Time Time::operator++(int)
{
    Time temp(*this);
    sec++;
        if (sec>=60)
        {
            sec-=60;
            ++min;
        }
        return temp;//返回的是自加前的对象
}
int main()
{
    Time time1(12,59),time2;
    cout<<"time1:";
    time1.display();
    ++time1;
    cout<<"time1++:";
    time1.display();
    time2=time1++;
    cout<<"time1++:";
    time1.display();
    cout<<"time2:";
    time2.display();
}

time1:12:59
time1++:13:0
time1++:13:1
time2:13:0
请按任意键继续. . .

Time operator++();//重载++i;
    Time operator++(int);//重载i++;

C++约定,后置自增/自减运算符增加一个int型参数。

posted on 2014-04-21 09:37  Yogurshine  阅读(715)  评论(0编辑  收藏  举报