+号重载
1)成员函数重载
如果要返回Person类,构造Person的时候就一定要赋初始值。
如果不赋予初始值,就一定要有默认初始值,因为提供了含参构造函数,编译器就不会再提供默认构造函数了。

#include<iostream>
#include<string>using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}Person operator+(Person p1){Person p;p.m_age = this->m_age + p1.m_age;p.m_money = this->m_money + p1.m_money;return p;}
};
int main(void){Person p1(10,200);Person p2(12,300);Person p3 = p1 + p2;cout << p3.m_age << " " << p3.m_money << endl;
}

2)全局函数重载

#include<iostream>
#include<string>using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}// Person operator+(Person p1){//     Person p;//     p.m_age = this->m_age + p1.m_age;//     p.m_money = this->m_money + p1.m_money;//     return p;// }
};
Person operator+(Person p1,Person p2){Person p0;p0.m_age = p1.m_age + p2.m_age;p0.m_money = p1.m_money + p2.m_money;return p0;
}
int main(void){Person p1(10,200);Person p2(12,300);Person p3 = p1 + p2;cout << p3.m_age << " " << p3.m_money << endl;
}

<<操作符重载
1)成员函数重载
为什么这个不能用成员函数编写?
假如写成Person的成员函数,

class Person p{void operator<<(Person &p){}//这样展开以后会是p.operator<<//造成了本来要在右边的对象现在放在了左边,不符合输出的条件,而且ostream对象应该在右边,所以不能写成成员函数,
}

2)全局函数重载

#include<iostream>
#include<string>
using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}
};ostream& operator<<(ostream &cout,Person &p){cout << "age : " << p.m_age << endl;cout << "money : " << p.m_money << endl;return cout;
}
//我这样写的问题在于只是将返回值设置为ostream对象可以链式编程,但是<<运算符所需要的两个对象我都没有传递进去
//注意的有两点
//1)是借助ostream类将其输出的,所以在传入的参数中需要有一个ostream类
//2)返回的时候必须返回引用,不能返回拷贝,具体的原理我不懂
//原因是流类的拷贝构造函数是私有的,不能被调用,而我们都知道当形参或返回值为对象类型时都要调用对象的拷贝构造函数进行对象的拷贝。
//真的是这样的吗?为什么要这样设计?
int main(void){Person p0(10,100);cout << p0 ;return 0;
}

++重载符
问题:
为什么不用全局函数而要用成员函数
一般情况下,前置递增符返回引用,后置递增符返回值;

前置递增符

#include<iostream>
#include<string>
using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}Person& operator++(){m_age++;m_money++;return *this;}//前置递增运算符返回引用,因为改变的是传入参数的本身,//返回引用使得可以接着改变,//如果返回值,即使本身被改变了,但是最终返回的值是拷贝后的结果//操作符重载的成员函数默认传递的是值吗?
};
ostream& operator<<(ostream &cout,Person &p){cout << "age : " << p.m_age << endl;cout << "money : " << p.m_money << endl;return cout;
}
int main(void){Person p0(10,100);cout << ++(++p0) ;cout << p0 ;return 0;
}

后置递增符

#include<iostream>
#include<string>
using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}Person operator++(int){//int为占位参数,用来区分是后置运算符重载Person p = *this;this->m_age++;this->m_money++;return p;}
};ostream& operator<<(ostream &cout,Person p){//ostream& operator<<(ostream &cout,const Person& p){//或者是改成const reference
//之前出错的原因是在<<操作符重载的时候Person类传入的是引用,为什么这样对于后置递增运算符不行,但是对于前置运算符两者都可以。cout << "age : " << p.m_age << endl;cout << "money : " << p.m_money << endl;return cout;
}
int main(void){Person p1(10,100);cout << p1++ << endl;return 0;
}

关系运算符重载!= 、==
1)!=重载

#include<iostream>
#include<string>
using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}bool operator!=(Person& p1){if(this->m_age != p1.m_age || this->m_money != p1.m_money){return true;}return false;}//关系运算符的返回值是布尔值
};
ostream& operator<<(ostream &cout,Person &p){cout << "age : " << p.m_age << endl;cout << "money : " << p.m_money << endl;return cout;
}
int main(void){Person p1(10,100);Person p2(10,100);if(p1 != p2){cout <<" p1 and p2 aren't equal." << endl;}else cout << "p1 and p2 are equal." << endl;return 0;
}

2)==运算符

#include<iostream>
#include<string>
using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}bool operator==(Person& p2){//可以传递引用吗?if(this->m_age == p2.m_age && this->m_money == p2.m_money)return true;else return false;}
};
ostream& operator<<(ostream &cout,Person &p){cout << "age : " << p.m_age << endl;cout << "money : " << p.m_money << endl;return cout;
}
int main(void){Person p1(10,100);Person p2(10,100);if(p1 == p2){cout <<" p1 and p2 are equal." << endl;}else cout << "p1 and p2 aren't equal." << endl;return 0;
}

函数调用运算符重载

#include<iostream>
#include<string>
using namespace std;
class Person{public:int m_age;int m_money;Person(int a = 0,int b = 0):m_age(a),m_money(b){}void operator()(Person p1){cout << "age :" << p1.m_age << endl;cout << " money :" << p1.m_money << endl;}
};
ostream& operator<<(ostream &cout,Person &p){cout << "age : " << p.m_age << endl;cout << "money : " << p.m_money << endl;return cout;
}
int main(void){Person p1(13,100);Person p3;//不是写成Person(p3),不是类重载函数调用运算符进行隐式调用,而是类的实例进行函数调用p3(p1);//通过匿名函数对象进行操作符重载的调用Person()(p1);return 0;
}

2021-08-13c++——类之操作符重载相关推荐

  1. 操作符重载——C/C++学习笔记

    此篇文章来自于网上,作为自己学习中的笔记,若有侵权行为,请告之,24小时之内必删除!下面就转入正题吧! 一.什么是操作符重载? 一看到重载,很容易就让人联想到成员函数重载,函数重载可以使名称相同的函数 ...

  2. 【C/C++学院】(8)全局函数和类成员函数转化/友元/操作符重载

    1.全局函数和类成员函数转化 全局函数和成员函数的相互转化:只需要修改一个指向本类的this指针: #include <iostream> using namespace std;clas ...

  3. 类与对象:类的6个默认成员函数: 构造函数、析构函数、拷贝构造函数、赋值操作符重载、默认拷贝构造与赋值运算符重载的问题、const成员函数、 取地址及const取地址操作符重载

    1.类的6个默认成员函数 如果一个类中什么成员都没有,简称为空类.任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数. 构造函数 析构函数 拷贝构造函数 赋值操作符重载 const成员函数 ...

  4. C++:运算符重载与类的赋值运算符重载函数

    目录 章节知识架构 一.运算符重载 1. 运算符重载的基本概念 代码段1 2.关于运算符重载的重要语法细则 二.运算符重载在类中的使用 三.类的默认成员函数:=重载函数(赋值运算符重载) 1.自定义= ...

  5. C++实现日期类(运算符重载)

    日期类的实现 经历前期C语言的学习,C语言的编程思路是面向过程的编程,将所需要实现的功能封装为每一个功能函数,在主函数中进行调用 C++编程思想是面向对象的编程,相比较于C语言的编程,它更具有更高的安 ...

  6. C++拾趣——有趣的操作符重载

    操作符重载是C++语言中一个非常有用的特性.它可以让我们比较优雅的简化代码,从而更加方便的编写逻辑. 为什么要使用操作符重载 一种常见的用法是重载<<运算符,让标准输出可以输出自定义的类型 ...

  7. C++——构造函数(拷贝构造,拷贝复制),析构函数,操作符重载

    C++--构造函数(拷贝构造,拷贝复制),析构函数,操作符重载 构造函数与析构函数:: 涉及构造函数还可以看这篇文章C++搞懂深拷贝初始化=与赋值=的区别 1.声明和定义构造函数和析构函数 构造函数在 ...

  8. C++中逗号操作符重载的分析

    1,关注逗号操作符重载后带来的变化: 2,逗号操作符: 1,逗号操作符(,)可以构成都好表达式:exp1, exp2, exp3, ..., expN 1,逗号表达式用于将多个表达式连接为一个表达式: ...

  9. C++基本操作符重载

    操作符重载指的是将C++提供的操作符进行重新定义,使之满足我们所需要的一些功能.比如类的加减乘除.我们可以定义类中的某一个成员变量进行加减乘除. 在C++中可以重载的操作符有: +  -  *  /  ...

最新文章

  1. freemarker中 感叹号、双感叹号、问号、双问号 的使用方法
  2. C语言 将程序运行的时间记录在记事本中,下次运行进行读取
  3. sublime text3中文文件名显示为框框,怎么解决
  4. python lib head,使用Python 2中的urllib2发出HTTP HEAD请求
  5. Debug pycharm: Error running *.py, cannot run program, No such file or pragram
  6. php选择排序算法原理_PHP排序算法之选择排序
  7. 使用Xtrabackup进行MySQL备份
  8. Django设置TIME_ZONE和LANGUAGE_CODE为中国区域
  9. 还有人不知道什么是AndroidX的吗?文末领取面试资料
  10. linux查看发起ddos攻击的ip,在Linux上使用netstat命令查证DDOS攻击的方法
  11. nginx subrequest演示示例程序
  12. magento 基本配置
  13. mysql 测试数据生成器_ApexSQL Generate(SQL测试数据生成器)
  14. 802.11无线wifi协议学习(二)
  15. 下载全球任意台站的连续地震数据
  16. pixel 3 Top Shot
  17. AtCoder Beginner Contest 178 C
  18. 鸡年年终总结汇报PPT模板
  19. Java实现生成并下载Excel文件
  20. Get IT技能百科库 50个领域轻松直达

热门文章

  1. PHP开发者的Linux学习之路
  2. Postgresql杂谈 06—Postgresql中的范围和数组类型
  3. Zoom:1 是什么鬼?有什么作用?
  4. 微信运动修改器python代码
  5. Warning: You are using macOS 13.We do not provide support for this pre-release version.
  6. 每日一题 编写程序实现彩票号码生成器
  7. hp2548无线服务器地址,无线直连可以这么玩_HP 2548_办公打印应用指南-中关村在线...
  8. 物联网的未来:无源传输网络
  9. numpy中resize和reshape的区别
  10. 同时打开多个.exe文件怎么解决