zoukankan      html  css  js  c++  java
  • 重载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型参数。

  • 相关阅读:
    格式布局
    tp框架之文件上传
    tp框架之验证码
    tp框架之自动验证表单
    tp框架之留言板练习
    tp框架之session
    tp框架之登录验证
    tp框架之函数调用
    tp框架之分页与第三方类的应用
    tp框架之AJAX
  • 原文地址:https://www.cnblogs.com/Yogurshine/p/3677782.html
Copyright © 2011-2022 走看看