上一篇我么讲解了运算符重载的知识,今天我们通过一个例子Data日期类来巩固运算符重载的知识。
本篇文章我们将实现下面下面这些函数接口:
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
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);
// ==运算符重载
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;
};我们可以采用多文件的形式储存:

构造函数其实就是初始化的一个过程,尤其注意的是对于日期的一个合法性的判断。但是因为月份的天数的细微差距,这里调用一个GetMonthDay()函数接口来完成:
int Date::GetMonthDay(int year, int month)
{
assert(year >= 0 && month < 13 && month>0);
//开辟一个静态区间,用来解决不同月份的天数不同
static int monthDayAraay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && isLeapYear(month))
{
return 29;
}
return monthDayAraay[month];
}这里因为月份对应的天数是固定的,所以我们选择将每个月的天数储存在一个静态数组中,这样一来就可以做到一次的开辟,之后可以进行多次的调用。
同时因为二月天数收年份影响,所以这里继续编写一个判断是都为闰年的函数接口isLeapYear(),如果十二月份并且是闰年则直接返回29:
bool isLeapYear(int year)
{
return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0);
}上面就是我们应该在构造函数中进行的日期是否合法的判断,所以我们最终的构造函数应该是:
Date::Date(int year, int month, int day)
{
if (year > 0 && month < 13 && month>0 && day > 0 && day <= GetMonthDay(year,month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "日期不合翻!" << endl;
exit(-1);
}
}判断两个日期是否相等很简单,只需要将年月日的比较结果进行与逻辑判断即可:
bool Date::operator == (const Date & d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}要想判断日期<日期:
bool Date::operator < (const Date& d)
{
if (_year < d._year
|| (_year == d._year && _month < d._month)
|| (_year == d._year && _month == d._month && _day < d._day)
)
{
return true;
}
else
{
return false;
}
}因为我们已经写了 < 和 <=的接口了,而 <= 逻辑包含了前两者,所以只需要任意满足一个即可返回true:
bool operator <= (const Date& d)
{
return *this < d || *this == d;
}显然 == 和 != 形成逻辑非关系,所有只需要对 == 取反即可:
bool operator != (const Date& d)
{
return !(*this == d);
}> 和 <= 构成逻辑非,因此只需要对<=取反即可。
bool operator>(const Date& d)
{
return !(*this <= d);
}>= 和 < 构成逻辑非,因此只需要对 < 取反即可:
bool operator>=(const Date& d)
{
return !(*this < d);
}由上面的实现我们可以知道只要有了 == 和 < 接口我们就可以通过直接调用这两个接口实现其他的接口。因为接口的代码量比较小,所以我们选择将其直接写在类中,因为在类中实现的函数,编译器会将其默认为内联函数(内联函数可以减少函数调用的开销)。
+= 的逻辑是给出一个日期和一个天数,然后计算这个日期加上天数后的新日期,也就是说要返回一个Date日期类:

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 > 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)
{
return *this += -day;
}
_day -= day;
while (_day <= 0) //注意这个边界条件
{
_month--;
if (_month == 0)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}同理:
Date Date::operator - (int day)
{
Date ret(*this);
ret -= day;
return ret;
}测试结果:

我们发现前置++和后置++的操作符都是++操作符,并且他们的返回类型,函数名,参数列表都一模一样。那么他们我们应该怎么区别呢?
这个问题其实编译器也考虑到了,因此编译器为了区别前置++和后置++,编译器规定,后置++(或后置--)的参数列表中多穿一个值。此时因为两个函数的参数列表的参数个数不同,两个函数就构成了函数重载。就可以区分两个操作符了。
需要注意的是,加的这个参数是为了区分前置和后置,因此传过来的值无所谓。我们一般可以省略写就直接只写数据类型。
因此,前置++就相当于是 +=1:
Date& operator++()
{
*this += 1;
return *this;
}前置--就相当于 -=1:
Date& operator--()
{
*this -= 1;
return *this;
}后置++先使用,因此要创建一个tmp存上,再自身++:
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}后置--先使用,因此要创建一个tmp存上,再自身--:
Date operator--(int)//加参数为了区分,与传过来的值无关
{
Date tmp(*this);
*this -= 1;
return tmp;
}测试结果:

注意哦!重载之后的后置--,同样是先赋值再--。
这个接口我们要实现一个日期与另一个日期之间相差多少天。如果是小日期-大日期,应该返回一个负数。因此我们要对这个小细节进行处理。
这里的实现逻辑是:
int Date::operator-(const Date& d)
{
int flag = 1;
Date max = *this;//默认认为第一大
Date min = d;
if (*this < d)
{
min = *this;
max = d;
flag = -1;
}
int n = 0;
while (min != max)
{
++n;
++min;
}
return n*flag;
}测试结果:

(本篇完)
完整代码在下面哦