一、日期类的实现
根据之前赋值运算符重载逻辑,我们现在来实现完整的日期类。
1.1 判断小于
上篇博客已经实现:
bool 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;
}
1.2 判断等于
bool operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
1.3 判断小于等于
- 根据1.1和1.2,我们可以直接复用。
bool operator<=(const Date& d)
{
return *this <= d || *this == d;
}
- 假如要判断
d1
是否小于等于d2
,那就是d1
相当于*this
,d2
相当于d
1.4 判断大于
- 在这里我们改变一下思路,不需要像判断小于那样if嵌套,正因为已经实现了小于,我们直接取反就可以实现大于了
bool operator>(const Date& d)
{
return !(*this <=d);
}
1.5 判断大于等于
这里小于取反
bool operator>=(const Date& d)
{
return !(*this < d);
}
1.6 判断不等于
这里等于取反
bool operator!=(const Date& d)
{
return !(*this == d);
}
1.7 获取月份天数
因为有闰年的存在,所以我们必须先实现获取月份天数才能确保加减天数
- 因为这里的获取月份天数需要被频繁调用,我们这里声明和定义就不分离了(本质就是内联函数inline)
- 放到静态区,好处是避免重复调用而导致数组重复生成
inline int GetMonthDay(int year,int month)
{
assert(month < 13 && month > 0);
//这里记得加上头文件#include<>
static int MonthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31 };
// 先判断月份
if (month == 2 && (((year % 100 == 0) && (year % 4 == 0)) || (year % 400 != 0)))
return 29;
return MonthDays[month];
}
1.8 日期加等天数
获取到月份天数后,我们就可以往下实现了。
- 首先加上天数,判断当前月的天数和加上的天数
- 然后进行减掉天数,月份+1,如果月份等于了13,年就+1,月份赋值为1
Date& operator+=(int day)
{
// 这里就直接修改了
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
1.9 日期加天数(本身不能改变)
- 与日期加等天数不同,这里需要另外开一块空间,修改别的空间才不会影响这里的值
- 这里不可以用引用返回(出了作用域还在才能使用引用返回),tmp是一个临时对象,必须用传值返回
Date operator+(int day)
{
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;
}
}
return tmp;
}
- +=和+很相似,所以可以用+复用+=
Date operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}
- 也可以用+=复用+
Date& operator+=(int day)
{
*this = *this + day;
return *this;
}
总体而言用+复用+=会更好,因为+里面会创建临时对象
1.10 日期减等天数
思路与上面加等一致
Date& operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
1.11 日期减天数
Date operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
二、前置++和后置++重载
- 前置++
Date& operator++()
{
*this += 1;
return *this;
}
- 为了与前置++区分,增加一个int形参,能够构成重载区分
- 后置++是要返回++以后的值
Date operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
三、日期-日期
int operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
int flag = -1;
max = d;
min = *this;
}
// 相差天数
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
四、const修饰
4.1 const成员函数
- 定义:将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << "Print()" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
void Print()
{
cout << "Print()const" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
};
void Test()
{
Date d1(2022,1,13);
d1.Print();
const Date d2(2022,1,13);
d2.Print();
}
我们一起来运行一下:
这里是const对象去调用非const成员函数
这里会出现一个权限放大的问题
因此参数要改为 const Date*
所以要解决这个问题,我们要在第二个Print成员函数处加上一个const,如下图:
(这里的const修饰的是this指针指向的内容)
- 下面图片为非const对象和const对象同时调用const成员函数
根据运行结果可以看到:非const对象是可以调用const成员函数的(因为这是权限的缩小)
既然非const对象和const对象都可以调用const成员函数,那我们是否可以将所有函数都加上const呢?
答案是不能的~
因为如果函数内部要被修改,那肯定是不能加的。
4.2 const修饰总结
成员函数如果是一个对成员变量只进行读访问的函数,一般加上const,这样const对象和非const对象都可以访问
成员函数如果是一个对成员变量进行读写访问的函数,不可以加上const,因为不能修改成员变量
下面集中总结4个问题:
- const对象可以调用非const成员函数吗? > 不可以(权限放大)
- 非const对象可以调用const成员函数吗? > 可以(权限缩小)
- const成员函数内可以调用其它的非const成员函数吗?> 不可以(权限放大)
- 非const成员函数内可以调用其它的const成员函数吗?> 可以(权限缩小)
五、取地址及const取地址操作符重载
前面几篇博客我们已经聊过前面4个默认成员函数,最后再来看看这最后两个吧~(了解即可)
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ;
int _month ;
int _day ;
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容。
六、日期类的实现【源码】
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
// 构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (!CheckInvalid())
{
cout << "构造日期非法" << endl;
}
}
// 判断等于
bool operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
// 判断小于
bool 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 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& operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
Date operator+(int day)
{
Date tmp(*this);
//Date tmp = *this;
tmp += day;
return tmp;
}
// 日期加天数
Date operator+(const Date& d)
{
Date tmp(*this);
tmp._day += d._day;
while (d._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
++tmp._month;
if (tmp._month == 13)
{
++tmp._year;
tmp._month = 1;
}
}
return tmp;
}
// 日期-=天数
Date& operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 日期减天数
Date operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
// 前置++
Date& operator++()
{
*this += 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
// 日期-日期
int operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
int flag = -1;
max = d;
min = *this;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
inline int GetMonthDay(int year, int month)
{
assert(month < 13 && month > 0);
// 放到静态区
static int MonthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
// 先判断月份
if (month == 2 && (((year % 100 == 0) && (year % 4 == 0)) || (year % 400 != 0)))
return 29;
return MonthDays[month];
}
// 拷贝构造
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
bool CheckInvalid()
{
if (_year <= 0
|| _month < 1
|| _month > 12
|| _day < 1
|| _day > GetMonthDay(_year,_month))
{
return false;
}
else
{
return true;
}
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
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.CheckInvalid())
{
cout << "输入非法日期,请重新输入" << endl;
}
else
{
break;
}
}
return in;
}