operator引用貌似本对象@_@

    friend ostream& operator<<(ostream& os,const 类名 &类定义的){os<<类定义的.成员<<endl;return os;}
    friend istream& operator>>(istream &is,类名 &类定义的){is>>类定义的.成员;return is;}
    TT &operator++()
    TT operator++(int)//再定义一个类定义的
    TT &operator--()
    TT &operator--(int)//在定义一个类定义的

友出引运<<出引os常类引用

友入引运>>入引is类引用

后置无引用,多int多定义一个

class Time
{
private :
     int h,m,s;
public :
     Time():h(0),m(0),s(0) {}
     friend ostream& operator<<(ostream& os, const Time &a)
     {
         if (a.h>=0&&a.h<24&&a.m>=0&&a.m<60&&a.s>=0&&a.s<60)
             os<<setw(2)<<setfill( '0' )<<a.h<< ":" <<setw(2)<<setfill( '0' )<<a.m<< ":" <<setw(2)<<setfill( '0' )<<a.s;
         else
             os<< "error!!!" ;
         return os;
     }
     friend istream& operator>>(istream& is,Time& a)
     {
         is >> a.h>>a.m>>a.s;
         return is;
     }
     Time& operator--()
     {
         if (h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60)
         {
             --s;
             if (s<0)
             {
                 s+=60;
                 --m;
             }
             if (m<0)
             {
                 m+=60;
                 --h;
             }
             if (h<0)
                 h+=24;
         }
         return * this ;
     }
     Time operator--( int )
     {
         Time q=(* this );
         if (h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60)
         {
             --s;
             if (s<0)
             {
                 s+=60;
                 --m;
             }
             if (m<0)
             {
                 m+=60;
                 --h;
             }
             if (h<0)
                 h+=24;
         }
         return q;
     }
     Time& operator++()
     {
         if (h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60)
         {
             ++s;
             if (s>=60)
             {
                 s-=60;
                 ++m;
             }
             if (m>=60)
             {
                 m-=60;
                 ++h;
             }
             if (h>=24)
                 h-=24;
         }
         return * this ;
     }
 
     Time operator++( int )
     {
         Time q=(* this );
         if (h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60)
         {
             s++;
             if (s>=60)
             {
                 s-=60;
                 m++;
             }
             if (m>=60)
             {
                 m-=60;
                 h++;
             }
             if (h>=24)
                 h-=24;
         }
         return q;
     }
};

转载于:https://www.cnblogs.com/TogetherLaugh/p/6582342.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值