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;
    }
};
posted on 2017-03-19 21:20  TogetherLaugh  阅读(153)  评论(1编辑  收藏  举报