再来说一说函数运算
运算符重载函数是用来体现运算符的多态性,在学习C语言的过程中,运算符全都是用来解决内置类型数据、逻辑、关系表达式间的运算,而在学习C++的过程中,我们了解了类与对象,就会思考到自定义类型的对象之间是否也能进行+-*/等多种运算呢?基于此种想法,C++官方研发出了运算符重载函数,就是为了让自定义类型也能做运算,体现出了语言的多态性。
目录
前言:
- class Date
- {
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
-
- void Print() {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
-
- //获取月份天数
- int GetMonthDay(int year, int month) {
- int MonthDay[13] = { 0,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;
- }
- return MonthDay[month];
- }
-
- //+=运算符函数重载
- 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++() {
- *this+=1;
- return *this;
- }
-
- //后置为了和前置区分,在后置上约定加个int
- Date operator++(int) {
- Date tmp(*this); //调用拷贝构造
- *this += 1;
- return tmp; //返回临时对象,也要调用拷贝构造
- }
-
- private:
-
- int _year;
- int _month;
- int _day;
- };
-
- int main() {
- Date d1(2022, 2, 2);
- Date d2(d1);
-
- cout << "d1的日期前置结果:" << endl;
- (++d1).Print(); //前置++
- d1.Print();
-
- cout << "d2的日期后置结果:" << endl;
- (d2++).Print();
- d2.Print();
-
- return 0;
- }
注意事项:
使用++运算符重载,需要用到+=的运算重载。
前置++中,this指针出了作用域已经不在了,但*this(d1)是整个main函数生命周期,仍在,所以用引用返回;而后置++中,tmp是临时拷贝对象,出了作用域不在了,不可用引用返回。
最后就是C++官方为了区分前置++与后置++的不同,专门为后置++的重载形参处多加了个int,这是约定好的,就好比双方谈好了,已经签了合同了,就不能再改了,所以大家不要以为能够换个char、double、short,这些写法都不对!!!
函数调用图示如下:


- class Date
- {
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
-
- void Print() {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
- //获取月份天数
- int GetMonthDay(int year, int month) {
- int MonthDay[13] = { 0,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;
- }
- return MonthDay[month];
- }
-
- Date& operator-=(int day) {
- _day -= day;
- while(_day<=0) {
- _month--;
- _day += GetMonthDay(_year, _month);
- if (_month == 0) {
- _month = 12;
- _year--;
- }
- }
- return *this;
- }
-
- Date operator-(int day) {
- Date tmp(*this); //调用拷贝构造
- tmp -= day;
- return tmp; //tmp出了作用域就不见了,所以不能用引用返回
- }
-
- Date& operator--() {
- *this -= 1;
- return *this;
- }
- Date operator--(int) {
- Date tmp(*this); //调用拷贝构造
- *this -= 1;
- return tmp; //返回时也要用拷贝构造
- }
- private:
- int _year;
- int _month;
- int _day;
- };
-
- int main() {
- Date d1(2022, 2, 2);
- Date d2(d1);
-
- cout << "d1的日期前置--结果:" << endl;
- (--d1).Print(); //前置++
- d1.Print();
-
- cout << "d2的日期后置--结果:" << endl;
- (d2--).Print();
- d2.Print();
- return 0;
- }
前后置的--与++同根同理,就不再过多解释了。

总结:在编写代码的习惯上要常用前置++或前置--,因为后置的总要比前置的多进行两次拷贝,在运行效率上不如前置!
流插入运算符为 “<<",它与cout一起用,作用就是输出变量的值,若要使用cout,就要包含头文件#include
- #include
- using namespace std;
-
- int main() {
- int a = 10, b = 20;
- cout << a<
- cout << b << endl;
- return 0;
- }

而我们若是要想输出类的对象呢?这时我们便需要对流插入运算符<<做重载了。
通过之前对赋值运算符=、==、+=、-=、++、--、<等多个运算符重载的学习来看,我们都是把这些运算符重载函数放到类内,好处就是方便用到this指针和成员变量,毕竟若是私有成员,类外不能访问到,所以我们先放在类内看看:
代码如图:
- class Date
- {
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
-
- //流插入运算符重载函数
- void operator<<(ostream& out) {
- cout << _year << "年" << _month << "月" << _day << "日" << endl;
- }
-
-
- void Print() {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
- private:
-
- int _year;
- int _month;
- int _day;
- };
-
- int main() {
- Date d1, d2;
- cout << d1; //报错
- cout << d2; //报错
-
-
- d1 << cout; //不报错
- d2<< cout; //不报错
- return 0;
- }
通过程序的编译,我们发现cout<

而d1<:

但我们已经写了<<的运算符重载函数了,为什么会出现这种情况?
之前,我曾经说过,类内的每个成员函数都有一个this指针,只不过它被隐藏起来了,形参中并没有它的身影,而且运算符重载的特性之一就是:
注意:成员函数的第一个参数就是隐藏的this指针,那么cout<

cout<形参不会有this指针,需要把重载函数<<放在类外。
优化后,代码如下:
- class Date
- {
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
-
- void operator<<(ostream& out) {
- cout << _year << "年" << _month << "月" << _day << "日" << endl;
- }
-
-
- void Print() {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
- private:
-
- int _year;
- int _month;
- int _day;
- };
-
- void operator<<(ostream& out ,const Date& d) {
- cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
- }
- int main() {
- Date d1, d2;
- cout << d1; //不报错了
- cout << d2;
- return 0;
- }
但此时有了一个老问题,类外函数对于私有成员无法访问,这时,我简单提一提C++的一个新内容——友元,友元的作用就是让类外的函数能够访问到类内的private、protected型成员,C++规定:类中有这两种访问限定修饰符的成员会把类外的一切函数都当作敌人,只有身上被标识过友元的函数,它们才会认为这是朋友并让其有权限进行访问。这就是友元的作用。
而成为友元的方式就是在类中写一句友元函数声明!
friend void operator<<(ostream& out, const Date& d);
这样处在类外的operator<<函数就能够对私有成员进行访问了,从而cout<

测试:

此外还有一个问题:
上面的cout<

代码如下:
- class Date
- {
- //友元声明
- friend ostream& operator<<(ostream& out, const Date& d);
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
-
- void operator<<(ostream& out) {
- cout << _year << "年" << _month << "月" << _day << "日" << endl;
- }
-
-
- void Print() {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
- private:
-
- int _year;
- int _month;
- int _day;
- };
-
- //有引用返回,支持链式访问
- ostream& operator<<(ostream& out, const Date& d) {
- cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
- return out;
- }
- int main() {
- Date d1(2022,10,11);
- Date d2(2020, 12, 12);
- Date d3(1999, 1, 15);
- cout << d1 <
-
- return 0;
- }
测试:

四.流提取运算符重载
流提取运算符为>>,其写法与operator<<有着异曲同工之妙,它与cin结合使用,而使用cin需要用到头文件#include,所以写法上只需要把ostream改为istream即可。
代码如下:
- class Date
- {
- friend istream& operator>>(istream& in, Date& d);
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
-
-
- void Print() {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
- private:
-
- int _year;
- int _month;
- int _day;
- };
-
- //有引用返回,支持链式访问
-
- istream& operator>>(istream& in, Date& d) {
- in >> d._year >> d._month >> d._day;
- return in;
- }
-
- int main() {
- Date d1,d2,d3;
- cin >>d1;
- //链式访问
- cin >>d2 >> d3;
-
- return 0;
- }
-
相关阅读:
Hbase的SQL接口之Phoenix使用心得
【Eclipse中的Preferences窗口中没有Server的解决方案】&【Eclipse配置Tomcat】
如何让Java的线程池顺序执行任务?
Docker中搭建Elasticsearch+Kibana
C语言程序设计笔记(浙大翁恺版) 第二周:计算
怎么压缩视频?视频过大跟我这样压缩
MySQL 存储过程创建指定表结构
软件测试 - 软件测试流程(完整版)避免当背锅侠,测试人的生存......
Java内部类(成员内部类、静态内部类、局部内部类、局部内部类)
信创之国产浪潮电脑+统信UOS操作系统体验1:硬件及软件常规功能支持情况介绍
-
原文地址:https://blog.csdn.net/weixin_69283129/article/details/127803427