日期类的实现

class Date
{
public:
    // 1.无参的构造函数
    // Date()
    // {
    // }
    // 2.缺省构造函数
    Date(int year = 1900, int month = 1, int day = 1)
    {
        if(year < 1900 || month < 0 || month > 12 || day < 0 || day > 31)
        {
            assert(0);
        }
        _year = year;
        _month = month;
        _day = day;
    }
    //3. 带参的构造函数
    // Date (int year, int month, int day)
    // {
    //     _year = year;
    //     _month = month;
    //     _day = day;
    // }
    // 4. 拷贝构造函数,创建一个对象
    Date(Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    // 5. 析构函数
    ~Date()
    {
    }
    // 6. 初始化列表
# if 0
    Date (int year = 1900, int month = 1, int day = 1)
        :_year(year)
         ,_month(month)
         ,_day(day)
    {
        cout << "初始化列表" << endl;
    }
# endif
    // 7. 赋值运算符重载
    // d2 = d3;
    // d2.operator(*this, d3) ==> this 指向d2, d代表的是d3
    Date& operator = (const Date& d)
    {
        this -> _year = d._year;
        this -> _month = d._month;
        this -> _day = d._day;
        return *this;
    }

    // d2 == d3
    // d2.operator(*this, d3)
    bool operator == (const Date& d) 
    {
        if(this -> _year == d._year && this -> _month == d._month && this -> _day == _day)
        {
            return true;
        }
        return false;
    }

    bool operator != (const Date d)
    {
        if(!(*this == d))
        {
            return true;
        }
        return false;
    }
    //d1 > d2
    //d1.operator > (this, d2)
    bool operator > (const Date& d) 
    {
        if(this -> _year > d._year)
        {
            return true;
        }
        if(this -> _year == d._year)
        {
            if(this -> _month > d._month)
            {
                return true;
            }
        }
        if(this -> _month == d._month)
        {
            if(this -> _day > d._day)
            {
                return true;
            }
        }
        return false;
    }
    //d2 < d3
    //d2.operator(this, d3)
    bool operator < (const Date& d) 
    {
        if(*this == d || *this > d)
        {
            return false;
        }
        return true;
    }
    // d2 >= d3
    bool operator >= (const Date& d)
    {
        if(*this > d || *this == d)
        {
            return true;
        }
        return false;
    }

    //d2 <= d3
    bool operator <= (const Date& d)
    {
        if(*this < d || *this == d)
        {
            return true;
        }
        return false;
    }

    //d2++
    Date& operator ++ () // 前置 
    {
        ++( this -> _day );
        if(this -> _day > GetMonthDay(this -> _year, this -> _month))
        {
            this -> _day = 1;
            ++(this -> _month);
            if((this -> _month) > 12)
            {
                ++(this -> _year);
                this -> _month = 1;
            }
        }
        return *this;
    }

    //d2++
    Date operator ++ (int) // 后置 
    {
        Date ret = (*this);
        ++( *this );
        return ret;
    }

    //2018-1-1
    Date& operator -- ()
    {
        --(this -> _day);
        if(this -> _day == 0)
        {
            (this -> _month)--;
            if(this -> _month == 0)
            {
                this -> _year -= 1;
                this -> _month = 12;
            }
            (this -> _day) += GetMonthDay(this -> _year, this -> _month);
        }
        return *this;
    }

    Date operator -- (int)//后置
    {
        Date ret = *this;
        --(*this);
        return ret;
    }

    Date& operator += (int day)
    {
        this -> _day = this -> _day + day;
        while(this -> _day > GetMonthDay(this -> _year, this -> _month))
        {
            this -> _day = this -> _day - GetMonthDay(this -> _year, this -> _month);
            this -> _month ++;
            if(this -> _month > 12)
            {
                this -> _month = 1;
                this -> _year ++;
            }
        }
        return *this;
    }

    //d1 + 30
    //d1.operator(this, day)
    Date operator + (int day) 
    {
        Date ret = *this;
        ret += day;
        return ret;
    }

    Date& operator -= (int day)
    {
        this -> _day -= day;
        while(this -> _day <= 0)
        {
            this -> _month --;
            if(this -> _month < 1)
            {
                this -> _month = 12;
                this -> _year --;
            }
            this -> _day += GetMonthDay(this -> _year, this -> _month);
        }
        return *this;
    }

    Date operator - (int day)
    {
        Date ret = *this;
        ret -= day;
        return ret;
    }

    // 2018-1-31  -  2018-1-1
    // d1.operator(this, d2)
    int operator - (Date& d)
    {
        int count = 0;//记录天数
        int flag = 1;
        if(*this < d )
        {
            swap(*this, d);
            flag = -1;
        }
        while(*this > d)
        {
            --(*this);
            ++count;
        }
        count =flag * count;
        if(flag == -1)
        {
            swap(*this, d);
        }
        return count;
    }

    //取地址运算符重载
    const Date* operator & () const
    {
        return this;
    }

    Date* operator & ()
    {
        return this;
    }

    friend ostream& operator << (ostream& out, const Date& d)
    {
        out << "year:" <<d._year << endl;
        out << "month:" << d._month << endl;
        out << "day: " << d._day << endl;
        return out;
    }

    friend istream& operator >> (istream& in, Date& d)
    {
        cout << "请分别输入年月日" << endl;
        in >> d._year;
        in >> d._month;
        in >> d._day;
        return in;
    }

    void Display()//展示日期类
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
    int GetMonthDay(int year, int month)
    {
        int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(( year %4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 ))
        {
            day[month] = day[2] + 1;
        }
        return day[month];
    }

private:
    int _year;
    int _month;
    int _day;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值