该函数的功能是实现日期的相关操作,包括:
1.计算一个日期加上任一天数后的日期;
2.计算一个日期减去任一天数后的日期;
3.计算俩日期之间相差的天数;
4.对日期加1,包括前置++和后置++;
5.对日期减1,包括前置--和后置--。
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1) //构造函数
:_year(year)
, _month(month)
, _day(day)
{
if (!IsInvalid(_year, _month, _day)) // 若不合法,this->IsInvalid(this)
{
assert(false);
cout << "非法日期" << endl;
}
}
Date(const Date& d){ //拷贝构造函数
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d){ //operator= 赋值运算符重载函数
if (*this != d){
this->_year = d._year;
this->_month = d._month;
this->_day = d._day