C++運算符重載

所謂重載,就是從新賦予新的含義,函數重載就是對一個已有的函數賦予新的含義,使之實現新功能。
運算符的重載主要存在兩種形式,一種是做爲類的成員函數進行使用,另外一種則是做爲類的友元函數進行使用。運算符的重載的形式爲:
   返回類型 operator 運算符符號(參數說明)
    {    
           //函數體的內部實現
    }

例如,可否用「+」號進行兩個複數的相加,在C++中不能在程序中直接用運算符「+」對複數進行相加運算,用戶必須本身設法實現複數相加。例如用戶能夠經過定義一個專門的函數來實現複數相加。
ios

 1 /*
 2 實現複數類中的運算符重載
 3 定義一個複數類重載運算符+、-、*、/,使之能用於複數的加減乘除。
 4 方案一:使用類的成員函數完成運算符的重載;
 5 */
 6 #include<iostream>
 7 using namespace std;
 8 
 9 class Complex
10 {
11 private:
12     double real;
13     double imag;
14 public:
15     Complex()
16     {
17         real = 0 ; imag = 0 ;
18     }
19     Complex(double r,double i)
20     {
21         real = r ; imag = i ;
22     }
23     Complex operator+(Complex &c2);
24     Complex operator-(Complex &c2);
25     Complex operator*(Complex &c2);
26     Complex operator/(Complex &c2);
27     void display();
28 };
29 
30 //下面定義成員函數
31 Complex Complex::operator+(Complex &c2) //定義重載運算符」+「的函數
32 {
33     return Complex(this->real+c2.real , this->imag+c2.imag);   //此處this->real就是c1.real
34 }
35 Complex Complex::operator-(Complex &c2)//定義重載運算符」-「的函數
36 {
37     return Complex(this->real-c2.real , this->imag-c2.imag);   //此處this->real就是c1.real
38 }
39 
40 Complex Complex::operator*(Complex &c2)  
41 {
42     return Complex(this->real*c2.real , this->imag*c2.imag);   //此處this->real就是c1.real  
43 }
44 
45 Complex Complex::operator/(Complex &c2)  
46 {
47     Complex c;      
48     c.real = (real*c2.real + imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);      
49     c.imag = (-real*c2.imag + imag*c2.real)/(c2.real*c2.real+c2.imag*c2.imag);     
50     return c;      
51 }   
52 
53 void Complex::display()
54 {
55     cout<<"("<<real<<","<<imag<<"i)"<<endl;
56 }
57 
58 int main(void)
59 {
60     Complex c1(3,4),c2(5,-10),c3;
61     cout<<"c1=";
62     c1.display();
63     cout<<"c2=";
64     c2.display();
65     c3=c1+c2;   // 此處至關於非重載運算符函數中的「c3=c1.Complex_add(c2)
66     //將運算符+重載爲類的成員函數後,編譯系統會將程序中的表達式c1+c2解釋爲:c1.operator+(c2)即以c2爲實參調用c1的運算符重載函數operator+(Complex&c2),operator+是類的一個函數名
67     cout<<"c1+c2=";
68     c3.display();
69     c3=c1-c2;
70     cout<<"c1-c2=";
71     c3.display();
72     c3=c1*c2;
73     cout<<"c1*c2=";
74     c3.display();
75     c3=c1/c2;
76     cout<<"c1/c2=";
77     c3.display();
78     return 0;
79 }
View Code

 

運算符重載運算符的運算規則
      ①運算符重載函數也是函數,重載的運算符不會改變運算符的優先級、結合型和參數的個數。
      ②重載運算符不能違反語言的語法規則。
      ③賦值運算符除外,重載運算符可由派生類繼承下去。
      ④重載運算符不能使用默認參數。
      ⑤運算符=、()、[]和->等操做符必須定義爲類的成員函數,將這些操做符定義爲友元函數將在編譯時標記爲錯誤。
     
 ⑥C++中不能重載的運算符只有5個:
       .  (成員訪問運算符)
       .* (成員指針訪問運算符)
       ∷ (域運算符)
       sizeof(長度運算符)
       ?: (條件運算符)

      由於前兩個運算符不能重載是爲了保證訪問成員的功能不能被改變,域運算符和sizeof運算符的運算對象是類型而不是變量或通常表達式,不具重載的特徵。總結:帶點'.'的都不能重載,new和delete神馬的也能夠重載!
      ⑦友元運算符的參數規則與類成員運算符的參數規則不一樣,一員運算符必須顯示地聲明一個參數,二員運算符必須顯示地聲明兩個參數。類成員運算符重載時,參數中隱含了一個this指針。(另外,C++規定,有的運算符(如賦值運算符、下標運算符、函數調用運算符)必須定義爲類的成員函數有的運算符則不能定義爲類的成員函數(如流輸入「>>」和流輸出「<<」運算符、類型轉換運算符))。
重載爲類的成員函數時,參數個數=原操做數個數-1(後置++、--除外),它能夠經過this指針自由地訪問本類的數據成員,能夠少寫一個函數的參數,但必需要求運算表達式的第一個參數(即運算符左側的操做數)是一個類對象。
重載爲類的友元函數時,參數個數=原操做數個數,且至少應該有一個自定義類型的形參。
ide

 

如何重載增量運算符 ++ 和 --
    運算符++和—有前置和後置兩種形式,若是不區分前置和後置,則使用operator++( )或operator--( )便可;不然,要使用operator++( )或operator--( )來重載前置運算符,使用operator++(int)或operator--(int)來重載後置運算符,調用時,參數int被傳遞給值0。以下列程序段:
函數

 

 1 //重載增量運算符 ++ 和 --
 2 #include<iostream>
 3 using namespace std;
 4 
 5 class Complex
 6 {
 7 private:
 8     int x;
 9     int y;
10 public:
11     Complex()    //定義構造函數
12     {
13         x = 0 ; y = 0 ;
14     }
15     Complex(int a,int b)    //構造函數重載
16     {
17         x = a ; y = b ;
18     }
19     void display();
20     Complex operator ++();      //前置自增方式
21     Complex operator ++(int);   //後置自增方式(增長一個int類型的形參)
22 };
23 
24 Complex Complex::operator ++()
25 {
26     x = x + 1;
27     y = y + 1;
28     return *this;
29 }
30 Complex Complex::operator ++(int)
31 {
32     Complex temp(*this);
33     x = x + 1;
34     y = y + 1;
35     return temp;
36 }
37 void Complex::display()  
38 {  
39     cout<<"("<<x<<","<<y<<")"<<endl;  
40 }
41 int main(void)
42 {
43     Complex c1(3,4),c2(5,-10),c3;
44     c3 = c1++;
45     cout<<"c1++=";
46     c3.display();
47     c3 = ++c2;
48     cout<<"++c2=";
49     c3.display();
50     return 0;
51 }
View Code

 

 

重載流輸入運算符和流輸出運算符
this

istream  類的對象cin;
ostream類的對象cout;
若是想直接用「<<」和「>>」輸出和輸入本身聲明的類型的數據,必須對它們重載,對「<<」和「>>」重載的函數形式以下:
istream & operator >> (istream &,自定義類 &);
ostream & operator << (ostream &,自定義類 &);

重載運算符「>>」的函數的第一個參數和函數的類型都必須是istream&類型,第二個參數是要進行輸入操做的類。
重載「<<」的函數的第一個參數和函數的類型都必須是ostream&類型,第二個參數是要進行輸出操做的類。
只能將重載「>>」和「<<」的函數做爲友元函數或普通的函數,而不能將它們定義爲成員函數。

實現代碼以下:
spa

 

 

 1 //增長重載流提取運算符「>>」,用「cin>>」輸入複數,用「cout<<」輸出複數。
 2 #include<iostream>
 3 using namespace std;
 4 
 5 class Complex
 6 {
 7 private:
 8     double real;
 9     double imag;
10 public:
11     Complex()    //定義構造函數
12     {
13         real = 0 ; imag = 0 ;
14     }
15     Complex(double r,double i)    //構造函數重載
16     {
17         real = r ; imag = i ;
18     }
19     friend istream& operator>>(istream & , Complex & );
20     friend ostream& operator<<(ostream & , Complex & );
21 };
22 
23 //下面定義友員函數 
24 istream& operator>>(istream & in, Complex &c)
25 {
26     cout<<"input real part and imaginary part of complex number:";
27     in>>c.real>>c.imag;
28     return in;
29 }
30 ostream& operator<<(ostream & out, Complex &c)
31 {
32     out<<'('<<c.real<<"+"<<c.imag<<"i)";
33     return out;
34 }
35 
36 int main(void)
37 {
38     Complex c1 , c2;
39     cin>>c1>>c2;
40     cout<<"c1="<<c1<<endl;
41     cout<<"c2="<<c2<<endl;
42     return 0;
43 }
View Code

 

轉載自http://blog.csdn.net/hackbuteer1/article/details/7554070.net

相關文章
相關標籤/搜索