zoukankan      html  css  js  c++  java
  • 对后置自增运算符的重载

    #include <iostream>
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    using namespace std;
    class Time
    {
        public:
            Time()
            {
                minute=0;
                sec=0;
            }
            Time(int m,int s):minute(m),sec(s){
            }
            Time operator++();
            Time operator++(int);
            void display()
            {
                cout<<minute<<":"<<sec<<endl;
            }
        private:
            int minute;
            int sec;
    };
    
    Time Time::operator++()
    {
        if(++sec>=60)
        {
            sec-=60;
            ++minute;
        }
        return *this;
    }
    
    Time Time::operator++(int)
    {
        Time temp(*this);
        sec++;
        if(sec>=60)
        {
            sec-=60;
            ++minute;
            return temp;
        }
    }
    int main(int argc, char** argv) {
        Time time1(34,59),time2;
        cout<<"time1:";
        time1.display();
        ++time1;
        cout<<"++time1:";
        time1.display();
        time2=time1++;
        cout<<"time1++";
        time1.display();
        cout<<"time2:";
        time2.display();
    }
  • 相关阅读:
    jdbc代码
    openwrt vsftp
    openwrt 配置samba && ubuntu 配置samba
    如何学习开源项目
    Makefile 笔记
    Samba 学习笔记
    quilt-补丁工具
    to-do-list
    新增feeds模块
    linux命令
  • 原文地址:https://www.cnblogs.com/borter/p/9405411.html
Copyright © 2011-2022 走看看