//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:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "非法日期->" << *this << endl;
}
}
int GetMonthDay(int year, int month) const
{
assert(month > 0 && month < 13);
static int monthDayArray[13] = { -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;
}
else {
return monthDayArray[month];
}
}
bool CheckDate()
{
if (_month < 1 || _month > 12 || _day < 1
|| _day > GetMonthDay(_year, _month))
{
return false;
}
return true;
}
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;
Date& operator-=(int day);
Date& operator+=(int day);
Date operator-(int day) const;
Date operator+(int day) const;
int operator-(const Date& d) const;
//++d1; --> d1.operator++();
Date& operator++();
//d1++; --> d1.operator++(1)
Date operator++(int);
Date& operator--();
Date operator--(int);
void Print() const;
private:
int _year;
int _month;
int _day;
};
//定义成inline后,发现不是小函数,那就做声明和定义分离
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
//inline ostream& operator<<(ostream& out, const Date& d)
//{
// out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
// return out;
//}
//inline istream& operator>>(istream& in, Date& d)
//{
// while (1)
// {
// cout << "请依次输入年月日:>";
// in >> d._year >> d._month >> d._day;
// if (!d.CheckDate())
// {
// cout << "输入日期非法,请重新输入!" << endl;
// }
// else {
// break;
// }
// }
// return in;
//}
分析及注意事项
- 类中函数定义在函数调用的下面为什么是可以的呢?
编译器在解析类定义的时候,会先处理所有成员函数的声明,然后再处理它们的实现,所以,成员函数之间的调用关系不受定义顺序的影响,如下示例。但是对于普通的函数就不行了。
class T
{
public:
void test()
{
cout << "test()" << endl;
test2();
}
void test2()
{
cout << "test2()" << endl;
}
private:
int _a;
};
int main()
{
T t1;
t1.test();
return 0;
}
-
析构、拷贝构造、赋值运算符重载不用自己写。因为日期类中没有申请资源,编译器自动生成的默认成员函数就够用。
-
势必要频繁调用,干脆直接定义在类里,成为内联函数,提高效率:
int GetMonthDay(int year, int month) const
{
//...
}
- 在“取月份天数”的函数中,判断闰年的条件逻辑更复杂,应该把简单条件放前面,这样可以提高程序运行的效率。同理,“判断日期是否合法”的函数中,也应该把简单判断的月份是否合法放在前面:
if (month == 2 && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
{}
if (_month < 1 || _month > 12 || _day < 1
|| _day > GetMonthDay(_year, _month))
{}
-
不是小函数定义在类里,而是频繁调用的小函数定义在类里成为内联函数。
-
尽量减少拷贝,日期类可能看不出来,大的类有申请资源,需要深拷贝的话,申请资源的空间也很大,这样的代价太大了。
-
前置是返回++之后的值,后置是返回++之前的值。
-
六个比较逻辑的函数只需要实现两个==和<,比较逻辑之间有互斥的关系,剩下的逻辑全部进行复用实现。
-
计算day天之前的日期与计算day天之后的日期分析:
完整代码
//Date.cpp
#include "Date.h"
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
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;
return tmp;
}
Date Date::operator+(int day) const
{
Date tmp(*this);
tmp += day;
return tmp;
}
//++d1
Date& Date::operator++()
{
*this += 1;
return *this;
}
//d1++
Date Date::operator++(int)
{
//Date tmp(*this);
Date tmp = *this;
*this += 1;
return tmp;
}
//--d1
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//d1--
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
//d1 - d2 --> *this - d
int Date::operator-(const Date& d) const
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (max != min)
{
++min;
++n;
}
return n * flag;
}
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
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
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);
}
void Date::Print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
while (1)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "输入日期非法,请重新输入!" << endl;
}
else {
break;
}
}
return in;
}