之前的学习中我们经常以日期类Date
作为例子帮助我们理解,但实际上我们之前实现的类是不规范的。一个规范的类,应该是声明和定义分开的,在有了前边知识的基础后,下面我们来实现一个规范的日期类Date
。
下面的代码实际上在之前的例子中都是出现过的,所以具体实现过程不再过多赘述。
21.1 结构设计
Date.h:函数和类的声明
Date.cpp:函数的实现
Test.cpp:测试类
21.2 完整代码
21.2.1 Date.h
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
explicit Date(int year = 1900, int month = 1, int day = 1);//一般规定在声明的地方给出缺省参数
int GetMonthDay(int year, int month) const;
Date& operator+=(int day);
Date operator+(int day) const;
Date& operator-=(int day);
Date operator-(int day) const;
Date& operator++();//前置++
Date operator++(int);//后置++
Date& operator--();//前置--
Date operator--(int);//后置--
int 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;
bool operator!=(const Date& d) const;
void Print();
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
21.2.2 Date.cpp
#include "Date.h"
int Date::GetMonthDay(int year, int month) const
{
int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
assert(month > 0 && month < 13);
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
else
{
return monthArray[month];
}
}
Date::Date(int year, int month, int day)
{
//检查日期是否合法
if (month > 0 && month < 13
&& (day > 0 && day <= GetMonthDay(year, month)))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期非法" << endl;
}
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= (-day);
return *this;
}
_day += day;
if (_day < 0)
{
}
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date tmp = *this;
/*tmp._day += day;
while (tmp._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
tmp._month++;
if (tmp._month == 13)
{
tmp._year++;
tmp._month = 1;
}
}*/
tmp += day;//复用operator+=
return tmp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
*this += (-day);
return *this;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
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)
{
Date max = *this;
Date min = d;
int flag = 1;
int n = 0;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
while (min != max)
{
min++;
n++;
}
return n;
}
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 _year < d._year
|| (_year == d._year && _month < d._month)
|| (_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);
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
void Date::Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
21.2.3 Test.cpp
#include "Date.h"
void TestDate1()//测试operator<<
{
cout << "TestDate1:测试<<" << endl;
Date d1(2023, 10, 5);
cout << d1;
}
void TestDate2()//测试>>
{
cout << "TestDate2:测试>>" << endl;
Date d1;
cin >> d1;
cout << d1;
}
void TestDate3()//测试+=、+、前置++、后置++
{
cout << "TestDate3.1:测试+=" << endl;
Date d1(2023, 1, 1);
cout << d1;
d1 += 30;
cout << "加上30天后为:" << d1;
cout << "TestDate3.2:测试+" << endl;
Date d2(2023, 1, 1);
cout << d2;
d2 = d1 + 30;
cout << "d1加上30天后为:" << d1;
cout << "TestDate3.3:测试前置++" << endl;
Date d3(2023, 1, 1);
cout << d3;
cout << ++d3;
cout << ++d3;
cout << "TestDate3.4:测试后置++" << endl;
Date d4(2023, 1, 1);
cout << d4;
cout << d4++;
cout << d4++;
}
void TestDate4()//测试-=、日期-天数、前置--、后置--、日期相-
{
cout << "TestDate4.1:测试-=" << endl;
Date d1(2023, 1, 1);
cout << d1;
d1 -= 1;
cout << "减上1天后为:" << d1;
cout << "TestDate4.2:日期减天数" << endl;
Date d2(2023, 1, 1);
cout << d2;
d2 = d2 - 1;
cout << "d1减1天后为:" << d1;
cout << "TestDate4.3:测试前置--" << endl;
Date d3(2023, 1, 1);
cout << d3;
cout << --d3;
cout << --d3;
cout << "TestDate4.4:测试后置--" << endl;
Date d4(2023, 1, 1);
cout << d4;
cout << d4--;
cout << d4--;
cout << "TestDate4.5:测试日期相减" << endl;
Date d5(2023, 1, 1);
Date d6(2023, 10, 1);
int ret = d6 - d5;
cout << d5;
cout << d6;
cout << "两个日期相差" << ret << "天";
}
void TestDate5()//测试==、<、<=、>、>=、!=
{
cout << "TestDate5.1:测试==" << endl;
Date d1;
Date d2;
cout << "d1:" << d1;
cout << "d2:" << d2;
if (d1 == d2)
{
cout << "d1 == d2" << endl;
}
cout << "TestDate5.2:测试<" << endl;
Date d3(2023, 1, 1);
Date d4(2023, 1, 2);
cout << "d3:" << d3;
cout << "d4:" << d4;
if (d3 < d4)
{
cout << "d3 < d4" << endl;
}
cout << "TestDate5.3:测试<=" << endl;
Date d5(2023, 1, 1);
Date d6(2023, 1, 1);
Date d7(2022, 12, 31);
cout << "d5:" << d5;
cout << "d6:" << d6;
cout << "d7:" << d7;
if (d5 <= d6)
{
cout << "d5 <= d6" << endl;
}
if (d7 <= d6)
{
cout << "d7 <= d6" << endl;
}
cout << "TestDate5.4:测试>" << endl;
Date d8(2023, 1, 1);
Date d9(2023, 1, 2);
cout << "d8:" << d8;
cout << "d9:" << d9;
if (d9 > d8)
{
cout << "d9 > d8" << endl;
}
cout << "TestDate5.5:测试>=" << endl;
Date d10(2023, 1, 1);
Date d11(2023, 1, 1);
Date d12(2022, 12, 31);
cout << "d10:" << d10;
cout << "d11:" << d11;
cout << "d12:" << d12;
if (d11 >= d10)
{
cout << "d11 >= d10" << endl;
}
if (d11 >= d12)
{
cout << "d11 >= d12" << endl;
}
cout << "TestDate5.6:测试!=" << endl;
Date d13(2023, 1, 1);
Date d14(2023, 1, 2);
cout << "d13:" << d13;
cout << "d14:" << d14;
if (d13 != d14)
{
cout << "d13 != d14" << endl;
}
}
int main()
{
TestDate5();
return 0;
}