完整呈现
Date.h
#include <iostream>
using namespace std;
//日期类
class Date
{
public:
int GetMonthDays(int year, int month) const;
//构造函数
Date(int year = 0, int month = 1, int day = 1);
//拷贝构造
Date(const Date& d);
//打印
void Print();
//析构函数
~Date();
//运算符的重载
bool operator==(const Date& d) const;
// = 运算符的重载
Date& operator=(const Date& d);
bool operator<(const Date& d) const;
bool operator<=(const Date& d)const;
bool operator>(const Date& d)const;
bool operator>=(const Date& d)const;
bool operator!=(const Date& d)const;
//日期+=天数,自己要改变
Date& operator+=(int day);
//日期加日期+,自己不改变
Date operator+(int day)const; //Date operaotor+(Date* this, int day)
//日期 -= 天数
Date& operator-=(int day);
//日期 - 天数
Date operator-(int day)const;
// 前置++ 后置++ // 都不能加const
Date& operator++();
Date operator++(int);
// 前置-- 后置--
Date& operator--();
Date operator--(int);
//日期 - 日期
int operator-(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
Date.cpp
#include "Date.h"
int Date::GetMonthDays(int year, int month)const
{
static int MonthDays[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 MonthDays[month];
}
//构造函数
Date::Date(int year, int month, int day)
{
if (year >= 0
&& month >= 1 && month <= 12
&& day >= 1 && day <= GetMonthDays(year, month))
{
this->_year = year;
this->_month = month;
this->_day = day;
}
else
{
cout << "非法日期" << endl;
}
}
//拷贝构造
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//打印
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//析构函数
Date::~Date()
{
;
}
//运算符的重载
bool Date:: operator==(const Date& d) const
{
return this->_year == d._year
&& _month == d._month
&& _day == d._day;
}
// = 运算符的重载
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) const
{
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;
else
return false;
}
bool Date:: operator<=(const Date& d)const
{
//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;
//else
// return false;
return *this < d || *this == d;
}
bool Date:: operator>(const Date& d)const
{
return !(*this <= d);
}
bool Date:: operator>=(const Date& d)const
{
return !(*this < d);
}
bool Date:: operator!=(const Date& d) const
{
return !(*this == d);
}
//日期+=天数,自己要改变
Date& Date:: operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDays(_year, _month))
{
_day -= GetMonthDays(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
//日期加日期+,自己不改变
Date Date:: operator+(int day)const //Date operaotor+(Date* this, int day)
{
/*Date ret(*this);
ret._day += day;
while (ret._day > GetMonthDays(ret._year, ret._month))
{
ret._day -= GetMonthDays(ret._year, ret._month);
++ret._month;
if (ret._month == 13)
{
++ret._year;
ret._month = 1;
}
}
return ret;*/
Date ret(*this);
ret += day; // ret.operator+=(day)
return ret;
}
//日期 -= 天数
Date& Date:: operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_month = 12;
_year = 1;
}
_day += GetMonthDays(_year, _month);
}
return *this;
}
//日期 - 天数 自身不改变
Date Date:: operator-(int day) const
{
//Date ret(*this);// Date ret = *this
//ret._day -= day;
//while (ret._day <= 0)
//{
// --ret._month;
// if (ret._month == 0)
// {
// ret._month = 12;
// --ret._year;
// }
// ret._day += GetMonthDays(ret._year, ret._month);
//}
//return ret;
Date ret = *this;
ret -= day;
return ret;
}
// 前置++ 后置++,默认前置++
Date& Date:: operator++()
{
*this += 1;
return *this;
}
Date Date:: operator++(int) //为了构成函数重载,后置++
{
Date tmp(*this);
*this += 1;
return tmp;
}
// 前置-- 后置--
Date& Date:: operator--()
{
*this -= 1;
return *this;
}
Date Date:: operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
//日期 - 日期
int Date:: operator-(const Date& d) const // const Date* this
{
int flag = 1;
Date max = *this; // 拷贝构造
Date min = d;
if (*this < d)
{
max = d; // operator=
min = *this;
flag = -1;
}
int n = 0;
while (max != min)
{
++min;
++n;
}
return n * flag;
}
Test.cpp
#include "Date.h"
int main()
{
Date d1(2024, 7, 16);
Date d2(2024, 7, 15);
d1.Print();
d2.Print();
cout << "-------------------" << endl;
// 运算符的重载
// ==
cout << (d1 == d2) << endl;
// <
cout << (d1 < d2) << endl;
// <=
cout << (d1 <= d2) << endl;
// >
cout << (d1 > d2) << endl;
//日期加日期 + +=
//Date d3(2024, 7, 15);
//d3.Print();
// d3.operator+(&d3,10);
//d3 += 1000;
//d3.Print();
//Date d4(2024, 7, 15);
//d4.Print();
//d4 -= 100;
//d4.Print();
Date d3(2024, 7, 15);
Date d4(2024, 7, 16);
cout << d4 - d3 << endl;
--d3;
d3.Print();
Date d5;
d5.Print();
return 0;
}
部分函数解读
日期 += 天数
1,当需要加的天数是正数的时候:
先用天数加上需要加的天数,然后用天数和当前月比较,如果超出了当前月的天数,就减去当前月的天数然后进一位月份,如果超出了12月,年就进一位,当天数小于当前月的天数的时候,就不需要进位了。
日期+天数也是同理,只不过多了一次拷贝构造一个临时对象,日期+天数保证自身不改变
2, 当日期是负数的时候,相对于日期 - 天数,即: 日期 - (-day), 在实现日期 -= 天数的基础上直接调用日期 -= 天数
Date& Date:: operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDays(_year, _month))
{
_day -= GetMonthDays(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
日期 -= 天数
1,当天数是正数的时候,天数相减如果是正数,一定满足在当前月,如果是负数,则不满足当前月,需要向前一个月借,如果还是负数,继续借,直接借到0月,没有0月,则去借上一年的12月,直接月份为正数。则满足当前月的天数
2, 负数和日期+=日期类似,如果日期 -= 负数 即相当于日期 += (-day), 在实现日期+=天数的基础上直接调用日期+=天数
Date& Date:: operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_month = 12;
_year = 1;
}
_day += GetMonthDays(_year, _month);
}
return *this;
}
最终总结与回顾
- 缺省参数:当声明有了缺省参数,定义的时候不需要再加缺省参数。
- const成员:const可以调用非const,const可以调用const,如果对象在函数内部不改变,则可以使用const修饰,如果在函数内部发生改变,则不能加const
- 引用返回:如果对象出了作用域还在,则可以使用引用返回,减少一次拷贝构造,如果对象出了作用域不在了,不能使用引用返回
- 日期类的实现,在有了类和对象的基础上实现并不复杂