• 重载i++,++i操作符


    #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型参数。

  • 相关阅读:
    二维hash(Uva 12886)
    C#中的线程(一)入门
    全国各地所有高校名单数据库 全国所有高校排名
    协议与代理
    表的约束条件
    na 斐波那契数列f(f(n))
    gcd题目
    Neighbor 隔壁
    hadoop
    Mybatis中实现mysql分页写法!!注意
  • 原文地址:https://www.cnblogs.com/Yogurshine/p/3677782.html
Copyright © 2020-2023  润新知