C++——运算符重载operator
C++ prime plus第11章,运算符重载是C++的一种多态。运算符重载格式如下:
operator运算符(argument-list)
1、做普通函数重载
示例:计算两个对象成员的和
#include <iostream>
using namespace std;
class A{
private:
int a;
public:
A(){
this->a=55;
}
int get_a(){
return this->a;
}
~A(){}
};
class B{
private:
int b;
public:
B(){
this->b=88;
}
int get_b(){
return this->b;
}
~B(){}
};
//计算两个对象成员的和
void operator+(A _A,B _B){
cout<<"A.a+B.b = "<<_A.get_a()+_B.get_b()<<endl;
}
int main (int argc,char *argv[]){
A _a;
B _b;
_a+_b;//加号前为函数重载第一个参数,加号后为第二个参数
return 0;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HYK0ePRI-1620203790036)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505153421516.png)]](https://i-blog.csdnimg.cn/blog_migrate/17d28376b202151319dd4fed43e98395.png#pic_center)
运算符前为函数重载第一个参数,运算符后为第二个参数,不能搞反。
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0FPbMGYU-1620203790039)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505152309822.png)]](https://i-blog.csdnimg.cn/blog_migrate/df59574014286e617bd3a21c13782172.png#pic_center)
2、做类成员函数重载
例如:重量计算
#include <iostream>
using namespace std;
class weight{
private:
int gram;
double kilogram;
int total_gram;
public:
weight(){
this->gram=10;
this->kilogram=1;
this->total_gram=0;
}
weight(int x,int y){
this->gram=x;
this->kilogram=y;
this->total_gram=0;
}
~weight(){}
int get_gram(){
return this->gram;
}
int get_kilogram(){
return this->gram;
}
int get_total_gram(){
return this->total_gram;
}
void operator+(weight A){
this->total_gram=this->gram+A.gram+1000*(this->kilogram+A.kilogram);
}
};
int main (int argc,char *argv[]){
weight A(500,2);
weight B(200,3);
A+B;//可以看到运算符前为被调用重载方法的对象,运算符后面是函数参数
cout<<A.get_total_gram()<<endl;
cout<<B.get_total_gram()<<endl;
return 0;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZUXUxAjJ-1620203790042)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505153554887.png)]](https://i-blog.csdnimg.cn/blog_migrate/7da8457790dc7747b262060a14f43171.png#pic_center)
可以看到运算符前为被调用重载方法的对象,运算符后面是函数参数
其实成员函数重载他的本质是:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AcJBMU3g-1620203790046)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505155357504.png)]](https://i-blog.csdnimg.cn/blog_migrate/467666e99888bfe7818ce9d646107ca4.png#pic_center)
3、运算符重载限制
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RmbSKEPK-1620203790048)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505161241760.png)]](https://i-blog.csdnimg.cn/blog_migrate/a1a5f43af4f475a2a4f67b20cec82fdb.png#pic_center)
![## [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xithMJh0-1620203790059)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505161307484.png)]](https://i-blog.csdnimg.cn/blog_migrate/d4b12e1faabb2f8b214b70582e4efbb2.png#pic_center)
4、++运算符与–运算符重载
成员函数
int operator++(){
this->gram++;
cout<<1<<endl;
return this->gram;
}
int operator++(int){
weight C=*this;
this->gram++;
cout<<2<<endl;
return C.gram;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2O2ppOt5-1620203790061)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505162345563.png)]](https://i-blog.csdnimg.cn/blog_migrate/5a39204f7645808557e53edca88897bf.png#pic_center)
友元函数
int operator--(weight &A){
A.gram--;
cout<<1<<endl;
return A.gram;
}
int operator--(weight &A,int){
weight D=A;
A.gram--;
cout<<2<<endl;
return D.gram;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wl1EqdTX-1620203790063)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505163131821.png)]](https://i-blog.csdnimg.cn/blog_migrate/3a6f11c0a05b48a609e90fc4a1e35b26.png#pic_center)
int main (int argc,char *argv[]){
weight A(500,2);
weight B(200,3);
A+B;
cout<<A.get_total_gram()<<endl;
cout<<B.get_total_gram()<<endl;
++A;
cout<<A.get_gram()<<endl;
cout<<A++<<endl;
cout<<A.get_gram()<<endl;
--A;
cout<<A.get_gram()<<endl;
cout<<A--<<endl;
cout<<A.get_gram()<<endl;
return 0;
}
下图后置方式必须用int作为伪参数,不能用其他类型
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ttVCdpv3-1620203790065)(C:\Users\wei\AppData\Roaming\Typora\typora-user-images\image-20210505163149282.png)]](https://i-blog.csdnimg.cn/blog_migrate/9d68a8ed91c58d79a602d19182f9e3cd.png#pic_center)
273

被折叠的 条评论
为什么被折叠?



