
◆ 博主名称: 晓此方-CSDN博客
大家好,欢迎来到晓此方的博客。
⭐️C++系列个人专栏:
目录
0.1概要&序論
这里是此方,久しぶりです! 今天,衔接上文的运算符重载 ,为大家带来時間计算器并补充更多的相关概念,其中就包括流绑定等重要内容 。希望本文能让你对运算符重载有更深的认识,「此方」です。让我们现在开始吧!
本项目由【测试文件test_of_Date.cpp,实现文件Date.cpp,和声明头文件date.h】三部分构成,以下主讲实现文件Date.cpp和Date.h部分,剩余的测试代码可由读者自行完成。
一,整体框架搭建
1.1一笔带过定义类、头文件、预处理、构造、析构等各种准备
cpp
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdbool.h>
using namespace std;
class Date
{
public:
//构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
// 析构函数
~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
// 拷贝构造函数
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
//..........
private:
int _year;
int _month;
int _day;
};
二,比较类运算符重载
2.1一笔带过重载"=="与"!="
cpp
bool operator==(const Date& d)
{
if (_year == d._year
&& _month == d._month
&& _day == d._day)
return true;
else
return false;
}
bool operator != (const Date& d)
{
if (*this == d)
{
return false;
}
else
{
return true;
}
}
2.2重载">"与">="
cpp
bool operator>(const Date& d)
{
if (_year < d._year)
{
return false;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return false;
}
else if (_month == d._month)
{
if (_day <= d._day)
{
return false;
}
}
}
return true;
}
- 比较两个Date对象的大小。
- 比较顺序为 年 → 月 → 日。
- 只要发现当前对象在任一层级上不可能大于d,就立即返回false。
- 所有情况都排除后,最终返回true,表示当前日期更晚。
cpp
bool operator >= (const Date& d)
{
if (*this == d)
{
return true;
}
else if (*this > d)
{
return true;
}
else
{
return false;
}
}
- 复用前者的逻辑,直接采用>号,>=就是>和=两种情况的合并。
- 否则返回false。
2.3重载"<"与"<="
cpp
bool operator < (const Date& d)
{
if (*this >= d)
{
return false;
}
else
{
return true;
}
}
- 复用2.2的逻辑,直接判断
cpp
bool operator <= (const Date& d)
{
if (*this > d)
{
return false;
}
else
{
return true;
}
}
- 复用2.2的逻辑,直接判断
三,加减运算符重载
3.1获取每月的天数
cpp
int GetMonthDay(int year, int month)
{
static int arrey[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
if ( month == 2&&year % 4 == 0 && year % 100 != 0 )
{
return 29;
}
else
{
return arrey[month];
}
}
- 如果直接采用case语句的话会大大增加代码量,所以采用数组+下标返回的方式如图。
- 小巧思:由于数组的下标从0开始,设置数组长度的时候多设置一个位置,让下标数字和月数一致便于理解
- 注意闰年的二月特殊处理
- 小巧思1:arrey[]数组建议静态化。放在静态区。避免频繁调用反复开辟栈空间。
- 小巧思2:在判断的时候把二月的判断放在前面,利用短路操作,如果不是二月不需要判断是不是闰年。
- 小巧思3:因为getmonyhday是一个非常频繁调用的函数,把这个函数放在类里面,默认是内联函数。
3.2日期+/+=天数
cpp
// 日期+=天数
Date& operator+=(int day)
{
_day += day;
while ( _day> GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
- 日期加等于天数,首先让当下的这个day+上天数的数字。
- 然后让这个day循环和当月的天数进行比较。
- 如果day大于天数,那么该day就减去这个天数并让月数+1,如果day小于天数。那么就跳出循环进行结算。(返回*this)
- 注意月数也有可能溢出,在月数=13的时候重置月数=1。
cpp
Date operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}
- 创建一个临时对象tmp并拷贝构造*this,+=day这个临时对象并返回
- 返回临时对象必须进行值返回,因为临时对象存放在栈区,离开operator作用域后销毁
3.3日期-/-=天数
cpp
// 日期-=天数
Date operator-=(int day)
{
_day -= day;
while ( _day< 0)
{
if (_month == 1)
{
_year--;
_month += 12;
}
_day += GetMonthDay(_year, _month-1);
_month--;
}
return *this;
}
- 整体思路与 += 对称,先把"减多少天"这件事落到当前对象上。
- 先执行 _day -= day,可能会出现当前天数不够用的情况。
- 当 _day < 0 时,说明需要向前借月份,于是回退到上一个月。
- 借月份时,将上一个月的天数加到 _day,同时 _month--。
- 如果月份退到 0,说明跨年:月份重置为 12,年份减 1。
- 重复上述过程,直到 _day >= 0,日期稳定,返回 *this。
cpp
Date operator-(int day)
{
Date tmp(*this);
tmp -= day;
return tmp;
}
- 基本同理
3.4前置和后置++
重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,无法很好地区分。C++规定,后置++重载时,增加一个int形参,跟前置++构成函数重载,方便区分。
cpp
// 前置++
Date& operator++()
{
*this += 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
- 前置 ++ 先对当前对象本身进行加 1 操作。
- 具体实现中直接复用 += 1,避免重复编写日期进位逻辑。
- 修改完成后,返回修改后的 *this,符合前置运算符"先变再用"的语义。
- 后置 ++ 需要先保留自增前的状态,因此先用当前对象构造一个临时副本 ret。
- 再对当前对象执行 += 1,使日期向后推进一天。
- 最后返回保存的旧值 ret,体现后置运算符"先用再变"的行为
注意:在自定义类型中建议多用前置++,减少拷贝。在内置类型中无所谓
3.5前置和后置--
cpp
// 后置--
Date operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
// 前置--
Date& operator--()
{
*this -= 1;
return *this;
}
- 同理,不多赘述。
3.6日期-日期:时间间隔
cpp
// 日期-日期 返回天数
int operator-(const Date& d)
{
int flag = 1;
int count = 0;
Date dmax = *this;
Date dmin = d;
if (dmax < dmin)
{
dmax = d;
dmin = *this;
flag = -1;
}
while (1)
{
count++;
dmin++;
if (dmin == dmax)
{
break;
}
}
return flag * count;
}
- 该运算符用于计算两个日期之间相差的天数,返回一个 int 结果。
- 默认认为当前对象较大,用 dmax、dmin 分别保存较大和较小的日期,同时用 flag 记录结果的正负。
- 如果发现当前对象早于参数 d,则交换 dmax 与 dmin,并将 flag 设为 -1。
- 从较小日期 dmin 开始,每次递增一天,同时将计数器 count 加 1。
- 当 dmin 增加到与 dmax 相等时,结束循环。
- 最终返回 flag * count,数值表示天数差,符号表示先后关系。
四,流重载
最复杂的运算符重载,着重讲解
首先我们要知道,流插入和流提取能够支持多种类型是因为其底层采用了大量的函数重载。

4.1检查函数
首先我们要写一个检查函数,后面要用
cpp
//检查
bool check_Date()
{
if (_day== GetMonthDay(_year,_month))
{
return false;
}
if (_month > 12)
{
return false;
}
return true;
}
- 检查当前日期是否合法。
- 若日期已达到当月最大天数,或月份超过 12,则判定为非法并返回 false;
- 否则说明日期处于合理范围内,返回 true。
4.2初步实现流插入
cpp
void Date::operator<<(ostream& out)
{
out << _year << "年" << _month << "月" << _day << "日" << endl;
}
-
首先流插入合流提取是ostream和istream类型的。
-
istream和ostream类型对象都不支持拷贝构造,所以不支持传值传递。
-
该代码确实实现了流插入,但是实现逻辑和使用方式上存在很大的问题。
-
如果我们要使用这个流插入,那么调用方式应该是:this<<cout,这和我们的使用习惯上由很大的出入。
-
**根本原因是什么?**是我们无法改变this指针的位置,this指针所在始终是运算符的左参数。
-
**解决办法:**把函数放在类外面。
-
**那么问题又又来了,怎么访问年月日?**难道要使用get一个一个访问吗?
-
实际上太麻烦了,这里简单引入一个概念,后面会深入讲:"友元函数":我是你的朋友,我可以访问你的私有空间
-
在类外面实现,并在类中设置一个友元声明。
cpp
friend ostream& operator<<(ostream& out, const Date d);
friend istream& operator>>(istream& in, Date& d);
好了,那么这就大功告成了吗?还没有,这个流掺入函数还不能实现连续输出。我们加一个返回值out,返回值实现连续输出的原理:

流插入操作符具有从左向右的结合性。cout<<d1的返回值作为<<d2的左操作数。以此类推。
最终,代码如下:
4.3实现流插入
cpp
//重载<<
ostream& operator<<(ostream& out, const Date d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
4.4实现流提取
- 流提取运算符的本质是:从 istream 中读取数据,并填充到对象中,因此第一个参数必须是 istream&。
- istream 不支持拷贝构造,所以只能以引用方式传递,返回值也必须是引用,才能支持连续输入。
- 该函数定义在类外,是为了符合使用习惯:cin >> d,而不是 d >> cin。
- 在函数内部直接读取 year、month、day。
- 通过 while 循环配合 check_Date,实现输入即校验:非法就提示并重新输入,合法才退出。
- 由于需要访问私有成员,该函数通常被声明为友元函数。
- 最终返回 in,以保证 cin >> d1 >> d2 这种链式写法成立。
cpp
//重载>>
istream& operator>>(istream& in, Date& d)
{
cout << "请输入" << endl;
while (1)
{
in >> d._year >> d._month >> d._day;
if (d.check_Date() == false)
{
cout << "日期非法,请重新输入" << endl;
}
else if (d.check_Date() == true)
{
break;
}
}
return in;
}
好了本期内容到此结束,如果对你有帮助不要忘了一键三联哦,我是此方,我们下期再见