前言
- 对于自定义类型对象的运算符重载,要和原生运算符有相同的功能,比如等号,可以连续赋值
- 以日期为例,日期之间的相加,没有意义;反之,日期之间的相减,则有意义。
- 成员函数之间的复用,实现几个主要的,其他调用微调即可;复用参考:调用拷贝构造函数次数小的函数。
日期类
- 判断输入的日期是否合法。
- 日期之间的加减法。
头文件
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
//获取当月的天数
int GetDays(int year, int month);
//判断构造函数初始化的日期是否合法
Date(int year = 0, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (_year < 0
|| _month <= 0 || _month >= 13
|| _day <= 0 || _day > GetDays(_year, _month))
{
cout << _year << "/" << _month << "/" << _day << "->";
cout << "非法日期" << endl;
}
}
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日\n";
}
Date& operator=(const Date d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//判断两个日期是否相等 需要改变实参的就用引用,不需要的就加const
bool operator==(const Date& d);
//判断比较两个日期的大小
bool operator<(const Date& d);
//> >=同理
bool operator>(const Date& d);
bool operator<=(const Date& d);
//+=天数
Date& operator+=(int);
//-=天数
Date& operator-=(int d);
//后置++加一天
Date& operator++();
//后置--,返回源来的,自减一
Date operator++(int d);
//加天数,减去天数同理
Date operator+(int d);
//减去日期相减
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
函数实现
#include"Dated.h"
//获取当月天数
int Date::GetDays(int year,int month)
{
assert(month > 0 && month < 13);
int ymd[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
return 29;
}
return ymd[month];
}
bool Date::operator==(const Date& d)
{
return d._year == _year &&
d._month == _month &&
d._day == _day;
}
bool Date::operator<(const Date& d)
{
if (_year<d._year)
{
return true;
}
else if(_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day<d._day)
{
return true;
}
return false;
}
//加上天数产生进位
Date& Date::operator+=(int d)
{
if (d<0)
{
return *this -= d;
}
_day += d;
while (_day>GetDays(_year,_month))
{
_day -= GetDays(_year, _month);
_month++;
if (_month>12)
{
++_year;
_month=1;
}
}
return *this;
}
Date& Date::operator-=(int d)
{
if (d<0)
{
return *this += d;
}
_day -= d;
while (_day<=0)
{
--_month;
if (_month <1)
{
--_year;
_month = 12;
}
//6. 8 - 30 = -22 + 31 = 5.9
_day += GetDays(_year, _month);
}
return *this;
}
//后置++加一天
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int d)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//加上天数
Date Date::operator+(int d)
{
Date tmp = *this;
tmp += d;
return tmp;
}
bool Date::operator>(const Date& d)
{
if (!(*this == d) && !(* this < d))
return true;
return false;
}
int Date::operator-(const Date& d)
{
//默认大小日期
Date max(*this);
Date min = d;
int flag = 1;
if (max < min)/比较函数那里已经处理了
{
Date tmp = max;
max=min;
min = tmp;
flag = -1;
}
//利用++函数,直到两个函数相等
int countd = 0;
while (min < max)
{
++countd;
++min;
}
return countd*flag;
}
bool Date::operator<=(const Date& d)
{
if (*this == d && !(*this > d))
{
return true;
}
else
{
return false;
}
}
大于当月天数,月就进位,再减去新月当月的天数;当天数小于1,则月减一位,天数加上当月天数…
测试代码
int main()
{
Date d1(2020,6,8);
Date d2(d1);
d2 -= 10;
d2.Print();
Date d3 = d1;
d3 = d2;
d3 += 100;
d3.Print();
d2 += 101;
// 输出操作符优先级较高
cout << (d1 < d2) << endl;
cout << (d1 == d2) << endl;
cout << (d3 > d2) << endl;
cout << "相差 " << (d2 - d3) << " 天 " << endl;
return 0;
}
perseverance