运算符的重载

函数名:operator 运算符
运算符重载之后不能改变运算符的优先级,结合性,运算符操作数的个数。
注:不能重载的运算符 指针访问运算符( .* ) ; 与运算符( :: ) ; 大小运算符( sizeof ) ; 条件运算符( ?: ) ; 成员运算符( . ).

代码多处使用复用,提高了代码的可维护性。

#include<iostream>
#include<assert.h>
#include<stdlib.h>
using namespace std;
class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)//构造函数
        : _year(year)           //初始化列表
        , _month(month)
        , _day(day)
    {

    }
    Date& operator=(const Date& d)//赋值运算符重载(将实例d的所有成员变量值全部赋值给this)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
        return *this;
    }
    ~Date()//析构函数
    {

    }

    int GetMonthDays()//获取每个月的天数
    {
        assert(_month > 0 && _month < 13);
        static int month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int days = month[_month];
        if (_month == 2 && IsLeapYear(_year))
        {
            return 29;
        }
        return days;
    }

    bool IsValid()//判断日期是否有效
    {
        if (_year >= 0 && _month > 0 && _month<13 && _day>0 && _day < GetMonthDays())
        {
            return true;
        }
        return false;
    }

    void Display()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    //声明函数
    bool operator>(const Date& d);
    bool operator>=(const Date& d);
    bool operator<(const Date& d);
    bool operator<=(const Date& d);
    bool operator==(const Date& d);
    bool operator!=(const Date& d);

    Date operator+(int day);
    Date& operator+=(int day);
    Date operator-(int day);
    Date& operator-=(int day);
    Date& operator++();
    Date operator++(int);
    Date& operator--();
    Date operator--(int);
    int operator-(const Date& d);

    bool IsLeapYear(int year);
    int GetMonthDays(int year, int month);

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

bool Date::operator>(const Date& d)//重载 >
{
    if (_year > d._year)
    {
        return true;
    }
    else if (_year == d._year)
    {
        if (_month > d._month)
        {
            return true;
        }
        else if (_month==d._month)
        {
            if (_day > d._day)
            {
                return true;
            }
        }
    }
}

bool Date::operator>=(const Date& d)//重载>=
{
    return (*this > d || *this == d);
}

bool Date::operator<(const Date& d)//重载<
{
    if (
        (_year < d._year) ||
        ((_year == d._year) && (_month < d._month)) ||
        ((_year == d._year) && (_month == d._month) && (_day < d._day))
        )
    {
        return true;
    }
    return false;
}

bool Date::operator<=(const Date& d)//重载<=
{
    return !(*this>d);
}

bool Date::operator==(const Date& d)//重载==
{
    return _year == d._year&&_month==d._month&&_day==d._day;

}

bool Date::operator!=(const Date& d)//重载!=
{
    return !(*this == d);
}

Date Date::operator+(int day)//给定日期加随机天数,*this不变
{
    if (day < 0)
    {
        return (*this - (-day));
    }

    Date ret(*this);
    ret._day += day;

    while (ret._day>ret.GetMonthDays())
    {
        ret._day -= ret.GetMonthDays();
        ret._month++;
        if (ret._month > 12)
        {
            ret._year++;
            ret._month = 1;
        }
    }

    return ret;
}

Date Date::operator-(int day)
{
    if (day < 0)
    {
        return (*this + (-day));
    }

    Date ret(*this);
    ret._day -= day;

    while (ret._day <= 0)
    {
        ret._month--;
        if (ret._month == 0)
        {
            ret._year--;
            ret._month = 12;
        }
        ret._day += ret.GetMonthDays();
    }

    return ret;
}

Date& Date::operator+=(int day)//*this会变成相加后的日期值
{
    *this = *this + day;//这里用到了复用,先调用*this.operator+,再调用赋值运算符=
    return *this;
}

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

Date& Date::operator++()//前置++,返回计算之后的值
{
    *this += 1;
    return *this;
}

Date Date::operator++(int)//后置++,返回原来的值
{
    Date ret(*this);
    *this += 1;
    return ret;
}

Date& Date::operator--()
{
    *this -= 1;
    return *this;
}

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

int Date::operator-(const Date& d)//小的日期进行++,每加一天就count加一,直到和大日期相等
{
    int flag = 1;//为了以防小日期减大日期
    Date big(*this);
    Date small(d);

    if (*this < d)
    {
        big = d;
        small = *this;
        int flag = -1;
    }

    int count = 0;
    while (small < big)
    {
        ++count;
        ++small;
    }
    return count*flag;
}

bool Date::IsLeapYear(int year)//判断是否是闰年
{
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
    {
        return true;
    }
    return false;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值