函数名: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;
}