c加加+-*/前置++后置++运算符重载

来源:互联网 发布:黑界扣字软件 编辑:程序博客网 时间:2024/06/10 15:07

运算符重载注意点:

1.算术和关系操作符返回的是一个左值或右值,而不是一个引用

2.赋值操作符一定要定义为成员函数如“=”

3.一般而言,赋值操作符和复合赋值操作符应返回左操作数的引用如"="和''+="

C++不允许赋值运算符被重载为全局形式,这是因为如果可以写出全局形式的赋值运算符函数的话,我们可以写出这样的函数:

[cpp] view plain copy
print?
  1. int operator=(int a, integer b);  

从而出现这样的语句:

[cpp] view plain copy
print?
  1. integer a(3);  
  2. 2 = a;  


现在我们写一个简单的integer类并重载赋值运算符:

因为在自赋值的情况下可能给对象造成伤害,所以在重载赋值运算符时必须要注意自赋值的情况

所以integer类中的赋值运算符函数应写成这样:

[cpp] view plain copy
print?
  1. integer& integer::operator=(const integer& a)  
  2. {     
  3.     if(this != &a)  
  4.     i = a.i;  
  5.     return *this;  
  6. };  


1、 运算符重载的概念

    运算符重载是C++的重要组成部分,它可以让程序更加简单易懂,简单的运算符可以使复杂函数的理解更直观。对于普通对象来说可以使用算术运算符让它们参与计算,C++也允许为类的对象构造运算符来实现单目或双目运输,这个特性就叫运算符的重载。其实,任何使用运算符完成的功能,使用普通的函数也能够完成。运算符的重载主要存在两种形式,一种是作为类的友元函数进行使用,另一种则是作为类的成员函数进行使用。运算符的重载的形式为:

2、 运算符的运算规则

[cpp] view plain copy
print?
  1. 返回类型 operator 运算符符号(参数说明)  
  2. {     //函数体的内部实现  
  3.   
  4. }  

 ①运算符重载函数也是函数,重载的运算符不会改变运算符的优先级、结合型和参数的个数。

 ②重载运算符不能违反语言的语法规则。

 ③赋值运算符除外,重载运算符可由派生类继承下去。

 ④重载运算符不能使用默认参数。

 ⑤运算符=()[]->可作为类成员运算符,不能作为友元运算符。

 ⑥运算符“.”、“::”、“?:”不能重载。

 ⑦友元运算符的参数规则与类成员运算符的参数规则不同,一员运算符必须显示地声明一个参数,二员运算符必须显示地声明两个参数。类成员运算符重载时,参数中隐含了一个this指针。

3、 实例代码
  1)友员

[cpp] view plain copy
print?
  1. #include <iostream>  
  2. using std::cout;  
  3. using std::endl;  
  4. class Complex  
  5. {  
  6. public:  
  7.     //Attribute  
  8.     int x;  
  9.     int y;  
  10.     //Operator  
  11.     void SetX(int a){x=a;}  
  12.     void SetY(int b){y=b;}  
  13.   
  14.     friend Complex operator +(Complex &, Complex &);  
  15.     friend Complex operator -(Complex &, Complex &);  
  16.     friend Complex operator *(Complex &, Complex &);  
  17.     friend Complex operator /(Complex &, Complex &);  
  18.   
  19.     friend Complex operator ++(Complex &);//前置方式  
  20.     friend Complex operator ++(Complex &, int);//后置方式  
  21. };  
  22. // "+"重载运算符  
  23. Complex operator +(Complex& temp1,Complex& temp2 )  
  24. {  
  25.     Complex ret;  
  26.     ret.x=temp1.x+temp2.x;  
  27.     ret.y=temp1.y+temp2.y;  
  28.     return ret;  
  29. }  
  30. // "-"重载运算符  
  31. Complex operator -(Complex& temp1,Complex& temp2 )  
  32. {  
  33.     Complex ret;  
  34.     ret.x=temp1.x-temp2.x;  
  35.     ret.y=temp1.y-temp2.y;  
  36.     return ret;  
  37. }  
  38. // "*"重载运算符  
  39. Complex operator *(Complex& temp1,Complex& temp2 )  
  40. {  
  41.     Complex ret;  
  42.     ret.x=temp1.x*temp2.x;  
  43.     ret.y=temp1.y*temp2.y;  
  44.     return ret;  
  45. }  
  46. // "/"重载运算符  
  47. Complex operator /(Complex& temp1,Complex& temp2 )  
  48. {  
  49.     Complex ret;  
  50.     ret.x=temp1.x/temp2.x;  
  51.     ret.y=temp1.y/temp2.y;  
  52.     return ret;  
  53. }  
  54. // "++"前置运算符  
  55. Complex operator ++(Complex& temp1)  
  56. {  
  57.     temp1.x=temp1.x+1;  
  58.     temp1.y=temp1.y+1;  
  59.     return temp1;  
  60. }  
  61. // "++"后置运算符  
  62. Complex operator ++(Complex& temp1,int)  
  63. {  
  64.     temp1.x=temp1.x++;  
  65.     temp1.y=temp1.y++;  
  66.     return temp1;  
  67. }  
  68. //主函数()  
  69. int main()  
  70. {  
  71.     Complex Complex1;  
  72.     Complex Complex2;  
  73.     Complex Ret;  
  74.   
  75.     Complex1.SetX(30);  
  76.     Complex1.SetY(40);  
  77.   
  78.     Complex2.SetX(10);  
  79.     Complex2.SetY(20);  
  80.   
  81.     cout<<"重载加法运算"<<endl;  
  82.     Ret=Complex1+Complex2;  
  83.     cout<<"Ret.x="<<Ret.x<<endl;  
  84.     cout<<"Ret.y="<<Ret.y<<endl;  
  85.   
  86.     cout<<"重载减法运算"<<endl;  
  87.     Ret=Complex1-Complex2;  
  88.     cout<<"Ret.x="<<Ret.x<<endl;  
  89.     cout<<"Ret.y="<<Ret.y<<endl;  
  90.   
  91.     cout<<"重载乘法运算"<<endl;  
  92.     Ret=Complex1*Complex2;  
  93.     cout<<"Ret.x="<<Ret.x<<endl;  
  94.     cout<<"Ret.y="<<Ret.y<<endl;  
  95.   
  96.     cout<<"重载除法运算"<<endl;  
  97.     Ret=Complex1/Complex2;  
  98.     cout<<"Ret.x="<<Ret.x<<endl;  
  99.     cout<<"Ret.y="<<Ret.y<<endl;  
  100.   
  101.     cout<<"前置++运算"<<endl;  
  102.     Ret=++Complex1;  
  103.     cout<<"Ret.x="<<Ret.x<<endl;  
  104.     cout<<"Ret.y="<<Ret.y<<endl;  
  105.   
  106.     cout<<"后置++运算"<<endl;  
  107.     Ret=Complex1++;  
  108.     cout<<"Ret.x="<<Ret.x<<endl;  
  109.     cout<<"Ret.y="<<Ret.y<<endl;  
  110.     return 0;  
  111. }  

2)非友员,相对于友员少了一个类对象参数

[cpp] view plain copy
print?
  1. #include <iostream>  
  2. using std::cout;  
  3. using std::endl;  
  4. class Complex  
  5. {  
  6. public:  
  7.     //Attribute  
  8.     int x;  
  9.     int y;  
  10.     //Operator  
  11.     void SetX(int a){x=a;}  
  12.     void SetY(int b){y=b;}  
  13.     //成员函数  
  14.     Complex operator +(Complex &);  
  15.     Complex operator -(Complex &);  
  16.     Complex operator *(Complex &);  
  17.     Complex operator /(Complex &);  
  18.     Complex& operator ++();//前置方式  
  19.     Complex& operator ++(int);//后置方式  
  20. };  
  21. // "+"重载运算符  
  22. Complex Complex::operator +(Complex& temp1)  
  23. {  
  24.     Complex ret;  
  25.     ret.x=x+temp1.x;  
  26.     ret.y=y+temp1.y;  
  27.     return ret;  
  28. }  
  29. // "-"重载运算符  
  30. Complex Complex::operator -(Complex& temp1)  
  31. {  
  32.     Complex ret;  
  33.     ret.x=x-temp1.x;  
  34.     ret.y=y-temp1.y;  
  35.     return ret;  
  36. }  
  37. // "*"重载运算符  
  38. Complex Complex::operator *(Complex& temp1)  
  39. {  
  40.     Complex ret;  
  41.     ret.x=x*temp1.x;  
  42.     ret.y=y*temp1.y;  
  43.     return ret;  
  44. }  
  45. // "/"重载运算符  
  46. Complex Complex::operator /(Complex& temp1)  
  47. {  
  48.     Complex ret;  
  49.     ret.x=x/temp1.x;  
  50.     ret.y=y/temp1.y;  
  51.     return ret;  
  52. }  
  53. // "++"前置运算符  
  54. Complex& Complex::operator ++()  
  55. {  
  56.     x=x+1;  
  57.     y=y+1;  
  58.     return *this;  
  59. }  
  60. // "++"后置运算符  
  61. Complex& Complex::operator ++(int)  
  62. {  
  63.     x=x++;  
  64.     y=y++;  
  65.     return *this;  
  66. }  
  67. //主函数()  
  68. int main()  
  69. {  
  70.     Complex Complex1;  
  71.     Complex Complex2;  
  72.     Complex Ret;  
  73.   
  74.     Complex1.SetX(30);  
  75.     Complex1.SetY(40);  
  76.   
  77.     Complex2.SetX(10);  
  78.     Complex2.SetY(20);  
  79.   
  80.     cout<<"重载加法运算"<<endl;  
  81.     Ret=Complex1+Complex2;  
  82.     cout<<"Ret.x="<<Ret.x<<endl;  
  83.     cout<<"Ret.y="<<Ret.y<<endl;  
  84.   
  85.     cout<<"重载减法运算"<<endl;  
  86.     Ret=Complex1-Complex2;  
  87.     cout<<"Ret.x="<<Ret.x<<endl;  
  88.     cout<<"Ret.y="<<Ret.y<<endl;  
  89.   
  90.     cout<<"重载乘法运算"<<endl;  
  91.     Ret=Complex1*Complex2;  
  92.     cout<<"Ret.x="<<Ret.x<<endl;  
  93.     cout<<"Ret.y="<<Ret.y<<endl;  
  94.   
  95.     cout<<"重载除法运算"<<endl;  
  96.     Ret=Complex1/Complex2;  
  97.     cout<<"Ret.x="<<Ret.x<<endl;  
  98.     cout<<"Ret.y="<<Ret.y<<endl;  
  99.   
  100.     cout<<"前置++运算"<<endl;  
  101.     Ret=++Complex1;  
  102.     cout<<"Ret.x="<<Ret.x<<endl;  
  103.     cout<<"Ret.y="<<Ret.y<<endl;  
  104.   
  105.     cout<<"后置++运算"<<endl;  
  106.     Ret=Complex2++;  
  107.     cout<<"Ret.x="<<Ret.x<<endl;  
  108.     cout<<"Ret.y="<<Ret.y<<endl;  
  109.     return 0;  
  110. }  
0 0