①date.h
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
Date(int year = , int month = , int day = )
:_year(year)
, _month(month)
, _day(day)
{
if (!IsInvalid())
{
assert(false);
}
}
Date(const Date& d) //拷贝构造
{
_year = d._year;
_month = d._month;
_day = d._day;
}
~Date(){}
bool IsInvalid();
void Show();
Date& operator=(const Date& d); //赋值运算符重载
bool operator==(const Date& d); //==重载
bool operator!=(const Date& d); //!=重载
bool operator>=(const Date& d); //>=重载
bool operator<=(const Date& d); //<=重载
// d1 < d2
bool operator>(const Date& d); //>重载
// d1 > d2
bool operator<(const Date& d); //<重载
// d1 + 10
Date operator+(int day); //+重载
Date& operator+=(int day); //+=重载
Date operator-(int day); //-重载
Date& operator-=(int day); //-=重载
//++d1
Date& operator++(); //前置++
//d1++
Date operator++(int); //后置++
Date& operator--(); //前置--
Date operator--(int); //后置--
int operator-(const Date& d); //计算两个日期相差天数
private:
int _year;
int _month;
int _day;
};
②date.cpp
#include "date.h"
void Date::Show()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool IsLeapYear(int year)
{
if (year % == && year % != || year % == )
{
return true;
}
return false;
}
int GetMonthDay(int year, int month)
{
int array[] = { -, , , , , , , , , , , , };
int day = array[month];
if (month == && IsLeapYear(year))
{
day = ;
}
return day;
}
bool Date::IsInvalid()
{
return _year >=
&& _month >= && _month <=
&& _day > && _day <= GetMonthDay(_year, _month);
}
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
bool Date::operator==(const Date& d) //隐含的this指针
{
return this->_year == d._year
&& this->_month == d._month
&& this->_day == d._day;
}
bool Date::operator!=(const Date& d)
{
return this->_year != d._year
|| this->_month != d._month
|| this->_day != d._day;
}
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
bool Date::operator<=(const Date& d)
{
return *this < d || *this == d;
}
bool Date::operator>(const Date& d)
{
return !(*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;
}
Date Date::operator+(int day) //本身并没有改变,不能返回引用
{
Date ret(*this);
ret._day += day;
while (ret._day > GetMonthDay(ret._year, ret._month))
{
int monthday = GetMonthDay(ret._year, ret._month);
ret._day -= monthday;
ret._month++;
if (ret._month > )
{
ret._month = ;
ret._year++;
}
}
return ret;
}
Date& Date::operator+=(int day)
{
*this = *this + day;
return *this;
}
Date Date::operator-(int day)
{
if (day < )
{
return *this + (-day);
}
Date ret(*this);
ret._day -= day;
while (ret._day < )
{
if (ret._month == )
{
ret._month = ;
ret._year--;
}
else
{
ret._month--;
}
int monthday = GetMonthDay(ret._year, ret._month);
ret._day += monthday;
}
return ret;
}
Date& Date::operator-=(int day)
{
*this = *this - day;
return *this;
}
Date& Date::operator++() // 前置(效率高)
{
*this += ;
return *this;
}
Date Date::operator++(int) // 后置
{
Date tmp(*this);
tmp = *this + ;
return tmp;
}
Date& Date::operator--()
{
*this -= ;
return *this;
}
Date Date::operator--(int)
{
Date tmp(*this);
tmp = *this - ;
return tmp;
}
int Date::operator-(const Date& d) //计算两个日期相差天数
{
//先确定哪个日期小,然后将小的往大的加,知道两个日期相等,就得到相差天数
int flag = ;
Date max(*this);
Date min(d);
if (*this < d)
{
min = *this;
max = d;
flag = -;
}
int days = ;
while (min < max)
{
min++;
days++;
}
return days * flag;
}
int main()
{
Date d1(, , );
d1.Show();
Date d2(, , );
d2 = d1;
d2.Show();
cout << "测试重载==:" << d1.operator==(d2) << endl;
cout << "测试重载!=:" << d1.operator!=(d2) << endl;
cout << "测试重载>:" << d1.operator>(d2) << endl;
cout << "测试重载<:" << d1.operator<(d2) << endl;
cout << "测试重载>=:" << d1.operator>=(d2) << endl;
cout << "测试重载<=:" << d1.operator<=(d2) << endl;
Date d3 = d2.operator+();
d3.Show();
d2.operator+=();
d2.Show();
Date d4 = d2.operator-(-);
d4.Show();
d2.operator-=();
d2.Show();
Date d5 = d2++;
d5.Show();
++d2;
d2.Show();
--d2;
d2.Show();
Date d6 = d2--;
d6.Show();
Date d(, , );
Date d7(, , );
d.Show();
d7.Show();
int days = d7 - d;
cout << " 相差天数:" << days << endl;
system("pause");
return ;
}