zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    测试常用的sql语句总结
    测试常用的Linux命令总结
    【转载】vim 中如何替换选中行或指定几行内的文本
    1074 Reversing Linked List
    1077 Kuchiguse
    LC 355. Design Twitter
    LCP 5. 发 LeetCoin
    LC 1409. Queries on a Permutation With Key
    1095 Cars on Campus
    LC 1369. Get the Second Most Recent Activity
  • 原文地址:https://www.cnblogs.com/TogetherLaugh/p/6582342.html
Copyright © 2011-2022 走看看