1. 程式人生 > >c加加+-*/前置++後置++運算子過載

c加加+-*/前置++後置++運算子過載

運算子過載注意點:

1.算術和關係操作符返回的是一個左值或右值,而不是一個引用

2.賦值操作符一定要定義為成員函式如“=”

3.一般而言,賦值操作符和複合賦值操作符應返回左運算元的引用如"="和''+="

C++不允許賦值運算子被過載為全域性形式,這是因為如果可以寫出全域性形式的賦值運算子函式的話,我們可以寫出這樣的函式:

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

從而出現這樣的語句:

[cpp] view plain
copy print?
  1. integer a(3);  
  2. 2 = a;  
integer a(3);
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. };  
integer& integer::operator=(const integer& a)
{   
    if(this != &a)
    i = a.i;
    return *this;
};

1、 運算子過載的概念

    運算子過載是C++的重要組成部分,它可以讓程式更加簡單易懂,簡單的運算子可以使複雜函式的理解更直觀。對於普通物件來說可以使用算術運算子讓它們參與計算,C++也允許為類的物件構造運算子來實現單目或雙目運輸,這個特性就叫運算子的過載。其實,任何使用運算子完成的功能,使用普通的函式也能夠完成。運算子的過載主要存在兩種形式,一種是作為類的友元函式進行使用,另一種則是作為類的成員函式進行使用。運算子的過載的形式為:

2、 運算子的運算規則

[cpp] view plain copy print?
  1. 返回型別 operator 運算子符號(引數說明)  
  2. {     //函式體的內部實現
  3. }  
返回型別 operator 運算子符號(引數說明)
{     //函式體的內部實現

}

 ①運算子過載函式也是函式,過載的運算子不會改變運算子的優先順序、結合型和引數的個數。

 ②過載運算子不能違反語言的語法規則。

 ③賦值運算子除外,過載運算子可由派生類繼承下去。

 ④過載運算子不能使用預設引數。

 ⑤運算子=()[]->可作為類成員運算子,不能作為友元運算子。

 ⑥運算子“.”、“::”、“?:”不能過載。

 ⑦友元運算子的引數規則與類成員運算子的引數規則不同,一員運算子必須顯示地宣告一個引數,二員運算子必須顯示地宣告兩個引數。類成員運算子過載時,引數中隱含了一個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.     friend Complex operator +(Complex &, Complex &);  
  14.     friend Complex operator -(Complex &, Complex &);  
  15.     friend Complex operator *(Complex &, Complex &);  
  16.     friend Complex operator /(Complex &, Complex &);  
  17.     friend Complex operator ++(Complex &);//前置方式
  18.     friend Complex operator ++(Complex &, int);//後置方式
  19. };  
  20. // "+"過載運算子
  21. Complex operator +(Complex& temp1,Complex& temp2 )  
  22. {  
  23.     Complex ret;  
  24.     ret.x=temp1.x+temp2.x;  
  25.     ret.y=temp1.y+temp2.y;  
  26.     return ret;  
  27. }  
  28. // "-"過載運算子
  29. Complex operator -(Complex& temp1,Complex& temp2 )  
  30. {  
  31.     Complex ret;  
  32.     ret.x=temp1.x-temp2.x;  
  33.     ret.y=temp1.y-temp2.y;  
  34.     return ret;  
  35. }  
  36. // "*"過載運算子
  37. Complex operator *(Complex& temp1,Complex& temp2 )  
  38. {  
  39.     Complex ret;  
  40.     ret.x=temp1.x*temp2.x;  
  41.     ret.y=temp1.y*temp2.y;  
  42.     return ret;  
  43. }  
  44. // "/"過載運算子
  45. Complex operator /(Complex& temp1,Complex& temp2 )  
  46. {  
  47.     Complex ret;  
  48.     ret.x=temp1.x/temp2.x;  
  49.     ret.y=temp1.y/temp2.y;  
  50.     return ret;  
  51. }  
  52. // "++"前置運算子
  53. Complex operator ++(Complex& temp1)  
  54. {  
  55.     temp1.x=temp1.x+1;  
  56.     temp1.y=temp1.y+1;  
  57.     return temp1;  
  58. }  
  59. // "++"後置運算子
  60. Complex operator ++(Complex& temp1,int)  
  61. {  
  62.     temp1.x=temp1.x++;  
  63.     temp1.y=temp1.y++;  
  64.     return temp1;  
  65. }  
  66. //主函式()
  67. int main()  
  68. {  
  69.     Complex Complex1;  
  70.     Complex Complex2;  
  71.     Complex Ret;  
  72.     Complex1.SetX(30);  
  73.     Complex1.SetY(40);  
  74.     Complex2.SetX(10);  
  75.     Complex2.SetY(20);  
  76.     cout<<"過載加法運算"<<endl;  
  77.     Ret=Complex1+Complex2;  
  78.     cout<<"Ret.x="<<Ret.x<<endl;  
  79.     cout<<"Ret.y="<<Ret.y<<endl;  
  80.     cout<<"過載減法運算"<<endl;  
  81.     Ret=Complex1-Complex2;  
  82.     cout<<"Ret.x="<<Ret.x<<endl;  
  83.     cout<<"Ret.y="<<Ret.y<<endl;  
  84.     cout<<"過載乘法運算"<<endl;  
  85.     Ret=Complex1*Complex2;  
  86.     cout<<"Ret.x="<<Ret.x<<endl;  
  87.     cout<<"Ret.y="<<Ret.y<<endl;  
  88.     cout<<"過載除法運算"<<endl;  
  89.     Ret=Complex1/Complex2;  
  90.     cout<<"Ret.x="<<Ret.x<<endl;  
  91.     cout<<"Ret.y="<<Ret.y<<endl;  
  92.     cout<<"前置++運算"<<endl;  
  93.     Ret=++Complex1;  
  94.     cout<<"Ret.x="<<Ret.x<<endl;  
  95.     cout<<"Ret.y="<<Ret.y<<endl;  
  96.     cout<<"後置++運算"<<endl;  
  97.     Ret=Complex1++;  
  98.     cout<<"Ret.x="<<Ret.x<<endl;  
  99.     cout<<"Ret.y="<<Ret.y<<endl;  
  100.     return 0;  
  101. }  
#include <iostream>
using std::cout;
using std::endl;
class Complex
{
public:
	//Attribute
	int x;
	int y;
	//Operator
	void SetX(int a){x=a;}
	void SetY(int b){y=b;}

	friend Complex operator +(Complex &, Complex &);
	friend Complex operator -(Complex &, Complex &);
	friend Complex operator *(Complex &, Complex &);
	friend Complex operator /(Complex &, Complex &);

	friend Complex operator ++(Complex &);//前置方式
	friend Complex operator ++(Complex &, int);//後置方式
};
// "+"過載運算子
Complex operator +(Complex& temp1,Complex& temp2 )
{
	Complex ret;
	ret.x=temp1.x+temp2.x;
	ret.y=temp1.y+temp2.y;
	return ret;
}
// "-"過載運算子
Complex operator -(Complex& temp1,Complex& temp2 )
{
	Complex ret;
	ret.x=temp1.x-temp2.x;
	ret.y=temp1.y-temp2.y;
	return ret;
}
// "*"過載運算子
Complex operator *(Complex& temp1,Complex& temp2 )
{
	Complex ret;
	ret.x=temp1.x*temp2.x;
	ret.y=temp1.y*temp2.y;
	return ret;
}
// "/"過載運算子
Complex operator /(Complex& temp1,Complex& temp2 )
{
	Complex ret;
	ret.x=temp1.x/temp2.x;
	ret.y=temp1.y/temp2.y;
	return ret;
}
// "++"前置運算子
Complex operator ++(Complex& temp1)
{
	temp1.x=temp1.x+1;
	temp1.y=temp1.y+1;
	return temp1;
}
// "++"後置運算子
Complex operator ++(Complex& temp1,int)
{
	temp1.x=temp1.x++;
	temp1.y=temp1.y++;
	return temp1;
}
//主函式()
int main()
{
	Complex Complex1;
	Complex Complex2;
	Complex Ret;

	Complex1.SetX(30);
	Complex1.SetY(40);

	Complex2.SetX(10);
	Complex2.SetY(20);

	cout<<"過載加法運算"<<endl;
	Ret=Complex1+Complex2;
	cout<<"Ret.x="<<Ret.x<<endl;
	cout<<"Ret.y="<<Ret.y<<endl;

	cout<<"過載減法運算"<<endl;
	Ret=Complex1-Complex2;
	cout<<"Ret.x="<<Ret.x<<endl;
	cout<<"Ret.y="<<Ret.y<<endl;

	cout<<"過載乘法運算"<<endl;
	Ret=Complex1*Complex2;
	cout<<"Ret.x="<<Ret.x<<endl;
	cout<<"Ret.y="<<Ret.y<<endl;

	cout<<"過載除法運算"<<endl;
	Ret=Complex1/Complex2;
	cout<<"Ret.x="<<Ret.x<<endl;
	cout<<"Ret.y="<<Ret.y<<endl;

	cout<<"前置++運算"<<endl;
	Ret=++Complex1;
	cout<<"Ret.x="<<Ret.x<<endl;
	cout<<"Ret.y="<<Ret.y<<endl;

	cout<<"後置++運算"<<endl;
	Ret=Complex1++;
	cout<<"Ret.x="<<Ret.x<<endl;
	cout<<"Ret.y="<<Ret.y<<endl;
	return 0;
}

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.     Complex1.SetX(30);  
  74.     Complex1.SetY(40);  
  75.     Complex2.SetX(10);  
  76.     Complex2.SetY(20);  
  77.     cout<<"過載加法運算"<<endl;  
  78.     Ret=Complex1+Complex2;  
  79.     cout<<"Ret.x="<<Ret.x<<endl;  
  80.     cout<<"Ret.y="<<Ret.y<<endl;  
  81.     cout<<"過載減法運算"<<endl;  
  82.     Ret=Complex1-Complex2;  
  83.     cout<<"Ret.x="<<Ret.x<<endl;  
  84.     cout<<"Ret.y="<<Ret.y<<endl;  
  85.     cout<<"過載乘法運算"<<endl;  
  86.     Ret=Complex1*Complex2;  
  87.     cout<<"Ret.x="<<Ret.x<<endl;  
  88.     cout<<"Ret.y="<<Ret.y<<endl;  
  89.     cout<<"過載除法運算"<<endl;  
  90.     Ret=Complex1/Complex2;  
  91.     cout<<"Ret.x="<<Ret.x<<endl;  
  92.     cout<<"Ret.y="<<Ret.y<<endl;  
  93.     cout<<"前置++運算"<<endl;  
  94.     Ret=++Complex1;  
  95.     cout<<"Ret.x="<<Ret.x<<endl;  
  96.     cout<<"Ret.y="<<Ret.y<<endl;  
  97.     cout<<"後置++運算"<<endl;  
  98.     Ret=Complex2++;  
  99.     cout<<"Ret.x="<<Ret.x<<endl;  
  100.     cout<<"Ret.y="<<Ret.y<<endl;  
  101.     return 0;  
  102. }