1> 思维导图
2> 整理代码
代码:
- #include
-
- using namespace std;
- class Person
- {
- //全局函数实现运算符重载需要权限
- friend const Person operator+(const Person L,const Person R);
- private:
- int a;
- int b;
- public:
- Person() {}
- Person(int a,int b){
- this->a=a;
- this->b=b;
- }
- void show(){
- cout << "a=" << a <<"b=" << b<
- }
- // 成员函数实现运算符重载
- // const Person operator+(const Person p)const{
- // Person temp;
- // temp.a=a+p.a;
- // temp.b=b+p.b;
- // return temp;
- // }
- const Person operator-(const Person p)const{
- Person temp;
- temp.a=a-p.a;
- temp.b=b-p.b;
- return temp;
- }
- const Person operator*(const Person p)const{
- Person temp;
- temp.a=a*p.a;
- temp.b=b*p.b;
- return temp;
- }
- const Person operator/(const Person p)const{
- Person temp;
- temp.a=a/p.a;
- temp.b=b/p.b;
- return temp;
- }
-
- };
- //全局函数实现运算符重载
- const Person operator+(const Person L,const Person R){
- Person temp;
- temp.a=L.a+R.a;
- temp.b=L.b+R.b;
- return temp;
- }
- int main()
- {
- Person p1(10,10);
- Person p2(10,20);
- Person p3=p1+p2;
- Person p4=p1-p2;
- Person p5=p1*p2;
- Person p6=p1/p2;
- p1.show();
- p2.show();
- p3.show();
- p4.show();
- p5.show();
- p6.show();
- return 0;
- }
关系运算符重载:
- #include
-
- using namespace std;
- class Person
- {
- //全局函数实现运算符重载需要权限
- friend bool operator>(const Person L,const Person R);
- private:
- int a;
- int b;
- public:
- Person() {}
- Person(int a,int b){
- this->a=a;
- this->b=b;
- }
- void show(){
- cout << "a=" << a <<"b=" << b<
- }
-
- // bool operator>(const Person p)const{
- // if(a>p.a && b>p.b){
- // return true;
- // }
- // return false;
- // }
- bool operator==(const Person p)const{
- if(a==p.a && b==p.b){
- return true;
- }
- return false;
- }
- bool operator<(const Person p)const{
- if(a
- return true;
- }
- return false;
- }
- bool operator>=(const Person p)const{
- if(a>=p.a && b>=p.b){
- return true;
- }
- return false;
- }
- bool operator<=(const Person p)const{
- if(a<=p.a && b<=p.b){
- return true;
- }
- return false;
- }
-
- };
- //全局函数实现运算符重载
- bool operator>(const Person L,const Person R){
- if(L.a>R.a && L.b>R.b)
- return true;
- return false;
- }
-
- int main()
- {
- Person p1(10,10);
- Person p2(10,20);
- p1.show();
- p2.show();
- if(p2>p1){
- cout << "p2>p1" <
- }
- if(p2==p1){
- cout << "p2==p1" <
- }
- if(p2
- cout << "p2
< - }
- if(p2>=p1){
- cout << "p2>=p1" <
- }
- if(p2<=p1){
- cout << "p2<=p1" <
- }
-
- return 0;
- }
赋值运算符重载
- #include
-
- using namespace std;
- class Person
- {
- //全局函数实现运算符重载需要权限
- friend Person &operator+=(Person &L,const Person &R);
- private:
- int a;
- int b;
- public:
- Person() {}
- Person(int a,int b){
- this->a=a;
- this->b=b;
- }
- void show(){
- cout << "a=" << a <<"b=" << b<
- }
- // Person &operator+=(const Person p){
- // a+=p.a;
- // b+=p.b;
- // return *this;
- // }
- Person &operator-=(const Person p){
- a-=p.a;
- b-=p.b;
- return *this;
- }
- Person &operator*=(const Person p){
- a*=p.a;
- b*=p.b;
- return *this;
- }
- Person &operator/=(const Person p){
- a/=p.a;
- b/=p.b;
- return *this;
- }
- Person &operator%=(const Person p){
- a%=p.a;
- b%=p.b;
- return *this;
- }
-
- };
- //全局函数实现运算符重载
- Person &operator+=(Person &L,const Person &R){
- L.a+=R.a;
- L.b+=R.b;
- return L;
- }
-
- int main()
- {
- Person p1(10,10);
- Person p2(10,20);
- p1.show();
- p2.show();
- // cout << "+=";
- // p2+=p1;
- // p2.show();
-
- // cout << "-=";
- // p2-=p1;
- // p2.show();
-
- // cout << "*=";
- // p2*=p1;
- // p2.show();
-
- // cout << "/=";
- // p2/=p1;
- // p2.show();
-
- cout << "%=";
- p2%=p1;
- p2.show();
-
- return 0;
- }
今日思维导图
-
相关阅读:
正则表达式高阶(一)
盲盒商城系统开发抽奖系统方案大全
随手记录第四话 -- elasticsearch基于docker安装以及SpringBoot集成使用
为什么iphone邮箱里已发送邮件是空的
【附源码】计算机毕业设计JAVA众筹平台网站
Android音视频开发-AudioTrack
C++之特殊数据成员
docker安装seata(指定配置文件、数据库、容器数据卷等)
手把手教你写一个JSON在线解析的前端网站1
Web项目如何做单元测试
-
原文地址:https://blog.csdn.net/weixin_53762703/article/details/133754944