C++ -- 运算符重载知识梳理

引言:


我们在C++进行符号运算时,比如说"+",可能我们从来都理所当然的用:

比如

int sum=0;

int a=1;

int b=2;

sum=a+b;


double sum=0;

double a=1;

double b=2;

double =a+b;


其实这是因为C++编译器对"+"运算符进行了重载,使它能进行不同类型的数据的运算。




 在运算符重载运用时应该注意以下几个问题:

1.C++只能重载已有的运算符,除个别几个,大部分都能重载。

2.重载运算符后其优先级和结合性不能改变。

3.运算符重载后功能性要和原来保持相似,不能有二义性。

4.重载后不能改变运算符的操作对象(操作数)的个数


重载的例子(转载他人写好的):


2 #include <iostream>
3
4 class Complex //复数类
5 {
6 private://私有
7 double real;//实数
8 double imag;//虚数
9 public:
10 Complex(double real=0,double imag=0)
11 {
12 this->real=real;
13 this->imag=imag;
14 }
15 friend Complex operator+(Complex com1,Complex com2);//友元函数重载双目运算符+
16 void showSum();
17 };
18
19
20 Complex operator+(Complex com1,Complex com2)//友元运算符重载函数
21 {
22 return Complex(com1.real+com2.real,com1.imag+com2.imag);
23 }
24
25 void Complex::showSum()
26 {
27 std::cout<<real;
28 if(imag>0)
29 std::cout<<"+";
30 if(imag!=0)
31 std::cout<<imag<<"i"<<std::endl;
32 }
33
34 int main()
35 {
36 Complex com1(10,10),com2(20,-20),sum;
37 sum=com1+com2;//或sum=operator+(com1,com2)
38 sum.showSum();//输出复数相加结果
39
40 return0;
41 }

但有一种情况,只能使用友元函数:

 class Complex //复数类
2 {
3 private://私有
4 double real;//实数
5 double imag;//虚数
6 public:
7 Complex(double real=0,double imag=0)
8 {
9 this->real=real;
10 this->imag=imag;
11 }
12 Complex operator+(int x);
13 };
14
15 Complex Complex::operator+(int x)
16 {
17 return Complex(real+x,imag);
18 }
19
20 int main()
21 {
22 Complex com1(5,10),total;
23 total=com1+5;
24
25 return0;
26 }

友元函数重载和类的成员函数重载区别:

主要是在重载函数的参数个数上,拿双目运算符来说:友元函数必须有全部的两个运算单元,而如果定义为类的成员函数就只需要一个,以此类推,单目运算中友元函数要有全部的一个运算单元,而成员函数则不需要运算单元。这主要是因为友元函数没有this指针,而类的成员函数是有的。


例:


 2 #include <iostream>
3
4 class Complex //复数类
5 {
6 private://私有
7 double real;//实数
8 double imag;//虚数
9 public:
10 Complex(double real=0,double imag=0)
11 {
12 this->real=real;
13 this->imag=imag;
14 }
15 Complex operator+(Complex com1);//成员函数重载双目运算符+
16 void showSum();
17 };
18
19
20 Complex Complex::operator+(Complex com1)
21 {
22 return Complex(real+com1.real,imag+com1.imag);
23 }
24
25 void Complex::showSum()
26 {
27 std::cout<<real;
28 if(imag>0)
29 std::cout<<"+";
30 if(imag!=0)
31 std::cout<<imag<<"i"<<std::endl;
32 }
33
34
35 int main()
36 {
37 Complex com1(10,10),com2(20,-20),sum;
38 sum=com1+com2;//或sum=com1.operator+(com2)
39 sum.showSum();//输出复数相加结果
40 return0;
41 }


 2 #include <iostream>
3
4 class Complex //复数类
5 {
6 private://私有
7 double real;//实数
8 double imag;//虚数
9 public:
10 Complex(double real=0,double imag=0)
11 {
12 this->real=real;
13 this->imag=imag;
14 }
15 friend Complex operator+(Complex com1,Complex com2);//友元函数重载双目运算符+
16 void showSum();
17 };
18
19
20 Complex operator+(Complex com1,Complex com2)//友元运算符重载函数
21 {
22 return Complex(com1.real+com2.real,com1.imag+com2.imag);
23 }
24
25 void Complex::showSum()
26 {
27 std::cout<<real;
28 if(imag>0)
29 std::cout<<"+";
30 if(imag!=0)
31 std::cout<<imag<<"i"<<std::endl;
32 }
33
34 int main()
35 {
36 Complex com1(10,10),com2(20,-20),sum;
37 sum=com1+com2;//或sum=operator+(com1,com2)
38 sum.showSum();//输出复数相加结果
39
40 return0;
41 }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值