要求实现日期类以及它的函数:
#include <iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in,Date& d);
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month) const;
bool CheckDate() const;
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数_其实不用实现
~Date()
{
}
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
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;
// !=运算符重载
bool operator != (const Date& d)const;
// 日期-日期 返回天数
int operator-(const Date& d)const;
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,Date& d);
感兴趣的读者,可以试着实现——文末有作者提供的参考代码和测试代码。
以下是作者写出来的bug(装作无事发生的淡然),咳咳,希望给作者自己以反思和读者们参考
1. 构造函数
重复定义默认参数

问题原因:在C++中,构造函数的默认参数只能在类的声明中定义一次。如果在类的声明中已经定义了默认参数,又在类的定义中再次定义,默认参数会被视为重复定义。

解决:确保默认参数只在类的声明中定义一次。

2. 关于++和--逻辑

序号则是它们执行的顺序,箭头所指即代码执行完后各自的结果。
首先是后置++,那么ret1的结果为d1。接着d1进行++,结果为2023-7-13。
与后置不同,前置++,首先d1的结果先加1:2023-7-14,再拷贝构造ret2——ret2结果自然为2023-7-14。
那么后置++:满足返回值为原先的值,而自身的值+1。
那么前置++:满足返回值为 +1后的自身。
// 后置++
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;// 而自身的值+1
return tmp;//返回值为原先的值,
}
// 前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
那么前置--和后置--都是一个道理:
前置--,返回自身-1后的自身即可。
后置--:返回自身的值,但是自身还需要-1
// 后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
// 前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
调试程序:观察结果

今天写日期类,改了两个小bug(☕)
日期类完整实现:
// Date.cpp
#include "Date.h"
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month) const
{
static int Days[] = {-1,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 Days[month];
}
bool Date::CheckDate() const
{
if (_month < 1 || _month > 12)
return false;
else if (_day < 1 || _day > GetMonthDay(_year, _month)) // 为什么这个如果不加const就会报错
return false;
else return true;
}
// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
//构造的时候判断合理性
if (!CheckDate())
{
cout << "该日期不合法: ";
}
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// >运算符重载
bool Date::operator>(const Date& d)const
{
if (_year != d._year) return _year > d._year;
else if (_month != d._month) return _month > d._month;
else return _day > d._day;
}
// ==运算符重载
bool Date::operator==(const Date& d) const
{
return _year == d._year && _month == d._month && _day == d._day;
}
// >=运算符重载
bool Date::operator >= (const Date& d)const
{
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)
{
// 注意day 的正负
if (day < 0) return *this -= (-day);
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
_month = 1;
++_year;
}
}
return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
Date tmp = *this;
tmp += day;
return tmp;
}
// 日期-天数
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
if (day < 0) return *this += (-day);
_day -= day;
while (_day <= 0)
{
--_month;// 日期往回过。逆生长
if (_month == 0)
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year,_month);
}
return *this;
}
// 前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
// 后置++
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
// 后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
// 前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
int Date::operator-(const Date& d)const
{
// 日期-日期
Date min = d;
Date max = *this;
int flag = 1; int ret = 0;
if (min > max)
{
min = *this;
max = d;
flag = -1;
}
while (min != max)
{
++min;
++ret;
}
return ret * flag;
}
// 自定义类型输入和输出
ostream& operator<<(ostream& out, const Date& d)
{
// 输出
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cout << "请输入年月日:>";
in >> d._year >> d._month >> d._day;
while (1)
{
if (!d.CheckDate())
{
cout << "日期不合法,请重新输入年月日:>";
in >> d._year >> d._month >> d._day;
}
else
break;
}
return in;
}
// test.cpp
#include "Date.h"
void test1()
{
Date d1;
d1 = Date(1900,8,19);
Date d2(2019,2,19);
cout << (d2 > d1) << endl;// 1
cout << (d2 == d1) << endl;// 0
cout << (d2 >= d1) << endl;// 1
cout << (d2 < d1) << endl;// 0
}
void test2()
{
Date d1;
d1 = Date(2023, 7, 12);
Date d2(2025, 6, 19);
//d1 += 10000;
//d2 -= 10000;
//Date d3 = d2 + 20000;
//Date d4 = d1 - 367;
Date ret1 = d1++;
Date ret2 = ++d1;
}
void test3()
{
Date d1 = Date(2024, 2, 29);
Date d2(2025, 2, 19);
cout << (d2 - d1) << endl;
cout << (d1 - d2) << endl;
}
void test4()
{
Date d1 = Date(2024, 2, 29);
Date d2(2025, 2, 19);
cout << d1 << d2;
Date d;
cin >> d;
cout << d;
}
int main()
{
//test1();// 测试运算符重载和构造函数日期
//test2();//测试+= -= + -
//test3();// 测试 日期- 日期
test4();//测试自定义输入和输出
return 0;
}