1.头文件
要点: (1)声明成员函数与成员变量
(2)针对Date类,构造函数需要自己写,系统不会进行处理,析构函数、拷贝构造函数与赋值重载可以不用写
(3)构造函数的声明与函数,缺省值只能写一处,下面代码中,构造函数的缺省值已经存在,则在函数中不能再存在
(4)前置++与后置++的声明不一样,后置++会多一个int形参,这个形参没有作用,只是为了区分
(5)运算符重载的&:参考拷贝构造函数中的讲解要点
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 0, int month = 1, int day = 1);
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日";
cout << endl;
}
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);
bool operator==(const Date& d);
inline bool operator >= (const Date& d);
bool operator < (const Date& d);
bool operator <= (const Date& d);
bool operator != (const Date& d);
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
2.函数源文件
要点: (1)每个月天数的存储:利用一个静态数组存储,每次出了函数,静态数组仍然存在,要调用时直接在静态存储区取,不用每次建立,提高效率
(2)判断闰年时的月份天数,先判断是否是2月,再判断是否是闰年,会提高不少效率
(3)把获取天数的函数设为一个内联函数,也能提高不少效率,获取天数是每次都会调用的,设为内联函数,提高速度
(4)此处的构造函数形参没有缺省值,与头文件中的构造函数有缺省值相对应
(5)检查日期的合法性,day <= GetMonthDay(year, month) 才合法
(6)日期+天数: operator+=:进位,加上后所给的day值改变了 ①加上后超过本月天数,月份+1 ②若月份加上后大于12,则进位,年份+1,且月份置为1 另外:要考虑所给的day < 0的情况 operator+:加上后所给的day值不变 复用operator+=代码时,要创建一个新的对象来相加 (operator-= 与operator-情况与上面一致,只是operator-=是借位,减去的是上一个月的天数)
(7)前置++与后置++区别:返回值不一样,前置++返回++之后的值,后置++返回++之前的值
(8)运算符重载:写出operator>与operator==后,其余利用代码复用
(9)日期-日期 返回天数:不能直接相减 ①假设一大一小,再比较,若比较与假设不一致,再交换值 ②设立flag = 1,是不知道哪个值大或者小,最后可以化解出现负数的情况 ③利用min与max不相等,逐渐相加,直到相等,因为中间会出现不同月份的天数,不好一个月一个月的相加
#include"date.h"
inline int GetMonthDay(int year, int month)
{
static int dayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = dayArray[month];
if (((month == 2) && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day = 29;
}
return day;
}
Date::Date(int year, int month, int day)
{
if (year >= 0
&& month > 0 && month < 13
&& day > 0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
cout << year << "年" << month << "月" << day << "日" << endl;
}
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= -day;
}
else
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month > 12)
{
++_year;
_month = 1;
}
}
}
return *this;
}
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
*this += -day;
}
else
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date ret = *this;
*this += 1;
return ret;
}
Date Date::operator--(int)
{
Date ret = *this;
*this -= 1;
return ret;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day > d._day)
return true;
}
}
return false;
}
bool Date::operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
inline bool Date::operator >= (const Date& d)
{
return *this > d || *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)
{
return !(*this == d);
}
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
min = *this;
max = d;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
3.测试源文件
#include"date.h"
void test1()
{
Date d1(2021, 5, 25);
d1.Print();
Date d2(2021, 0, 0);
Date d3(2021, 2, 29);
d3.Print();
}
void test2()
{
Date d1(2021, 5, 25);
d1.Print();
d1 += 7;
d1.Print();
Date d2 = d1 + 100;
d2.Print();
}
void test3()
{
Date d1(2021, 5, 27);
d1.Print();
d1 -= 100;
d1.Print();
Date d2(2021, 5, 27);
d2 -= -100;
d2.Print();
}
void test4()
{
Date d1(2021, 5, 27);
d1.Print();
++d1;
d1.Print();
Date ret1 = d1++;
d1.Print();
ret1.Print();
}
void test5()
{
Date d1(2021, 5, 27);
d1.Print();
--d1;
d1.Print();
Date ret2 = d1--;
d1.Print();
ret2.Print();
}
void test6()
{
Date d1(2021, 5, 27);
Date d2(2021, 12, 31);
cout << d1 - d2 << endl;
cout << d2 - d1 << endl;
}
int main()
{
test6();
return 0;
}
|