《Cracking the Coding Interview程式設計師面試金典》----C++過載>>和

塵封的記憶0發表於2017-04-19
在C++中,標準庫本身已經對左移運算子<<和右移運算子>>分別進行了過載,使其能夠用於不同資料的輸入輸出,但是輸入輸出的物件只能是 C++ 內建的資料型別(例如 bool、int、double 等)和標準庫所包含的類型別(例如 string、complex、ofstream、ifstream 等)。

如果我們自己定義了一種新的資料型別,需要用輸入輸出運算子去處理,那麼就必須對它們進行過載。本節以前面的 complex 類為例來演示輸入輸出運算子的過載。
其實 C++ 標準庫已經提供了 complex 類,能夠很好地支援複數運算,但是這裡我們又自己定義了一個 complex 類,這樣做僅僅是為了教學演示。
本節要達到的目標是讓複數的輸入輸出和 int、float 等基本型別一樣簡單。假設 num1、num2 是複數,那麼輸出形式就是:

cout<<num1<<num2<<endl;

輸入形式就是:

cin>>num1>>num2;

cout 是 istream 類的物件,cin 是 ostream 類的物件,要想達到這個目標,就必須以全域性函式(友元函式)的形式過載<<>>,否則就要修改標準庫中的類,這顯然不是我們所期望的。

過載輸入運算子>>

下面我們以全域性函式的形式過載>>,使它能夠讀入兩個 double 型別的資料,並分別賦值給複數的實部和虛部:
  1. istream & operator>>(istream &in, complex &A){
  2. in >> A.m_real >> A.m_imag;
  3. return in;
  4. }
istream 表示輸入流,cin 是 istream 類的物件,只不過這個物件是在標準庫中定義的。之所以返回 istream 類物件的引用,是為了能夠連續讀取複數,讓程式碼書寫更加漂亮,例如:

complex c1, c2;
cin>>c1>>c2;

如果不返回引用,那就只能一個一個地讀取了:

complex c1, c2;
cin>>c1;
cin>>c2;

另外,運算子過載函式中用到了 complex 類的 private 成員變數,必須在 complex 類中將該函式宣告為友元函式:

friend istream & operator>>(istream & in , complex &a);

>>運算子可以按照下面的方式使用:

complex c;
cin>>c;

當輸入1.45 2.34↙後,這兩個小數就分別成為物件 c 的實部和虛部了。cin>> c;這一語句其實可以理解為:

operator<<(cin , c);

過載輸出運算子<<

同樣地,我們也可以模仿上面的形式對輸出運算子>>進行過載,讓它能夠輸出複數,請看下面的程式碼:
  1. ostream & operator<<(ostream &out, complex &A){
  2. out << A.m_real <<" + "<< A.m_imag <<" i ";
  3. return out;
  4. }
ostream 表示輸出流,cout 是 ostream 類的物件。由於採用了引用的方式進行引數傳遞,並且也返回了物件的引用,所以過載後的運算子可以實現連續輸出。

為了能夠直接訪問 complex 類的 private 成員變數,同樣需要將該函式宣告為 complex 類的友元函式:

friend ostream & operator<<(ostream &out, complex &A);

綜合演示

結合輸入輸出運算子的過載,重新實現 complex 類:
  1. #include <iostream>
  2. using namespace std;
  3. class complex{
  4. public:
  5. complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
  6. public:
  7. friend complex operator+(const complex & A, const complex & B);
  8. friend complex operator-(const complex & A, const complex & B);
  9. friend complex operator*(const complex & A, const complex & B);
  10. friend complex operator/(const complex & A, const complex & B);
  11. friend istream & operator>>(istream & in, complex & A);
  12. friend ostream & operator<<(ostream & out, complex & A);
  13. private:
  14. double m_real; //實部
  15. double m_imag; //虛部
  16. };
  17. //過載加法運算子
  18. complex operator+(const complex & A, const complex &B){
  19. complex C;
  20. C.m_real = A.m_real + B.m_real;
  21. C.m_imag = A.m_imag + B.m_imag;
  22. return C;
  23. }
  24. //過載減法運算子
  25. complex operator-(const complex & A, const complex &B){
  26. complex C;
  27. C.m_real = A.m_real - B.m_real;
  28. C.m_imag = A.m_imag - B.m_imag;
  29. return C;
  30. }
  31. //過載乘法運算子
  32. complex operator*(const complex & A, const complex &B){
  33. complex C;
  34. C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag;
  35. C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag;
  36. return C;
  37. }
  38. //過載除法運算子
  39. complex operator/(const complex & A, const complex & B){
  40. complex C;
  41. double square = A.m_real * A.m_real + A.m_imag * A.m_imag;
  42. C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square;
  43. C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square;
  44. return C;
  45. }
  46. //過載輸入運算子
  47. istream & operator>>(istream & in, complex & A){
  48. in >> A.m_real >> A.m_imag;
  49. return in;
  50. }
  51. //過載輸出運算子
  52. ostream & operator<<(ostream & out, complex & A){
  53. out << A.m_real <<" + "<< A.m_imag <<" i ";;
  54. return out;
  55. }
  56. int main(){
  57. complex c1, c2, c3;
  58. cin>>c1>>c2;
  59. c3 = c1 + c2;
  60. cout<<"c1 + c2 = "<<c3<<endl;
  61. c3 = c1 - c2;
  62. cout<<"c1 - c2 = "<<c3<<endl;
  63. c3 = c1 * c2;
  64. cout<<"c1 * c2 = "<<c3<<endl;
  65. c3 = c1 / c2;
  66. cout<<"c1 / c2 = "<<c3<<endl;
  67. return 0;
  68. }
執行結果:
2.4 3.6↙
4.8 1.7↙
c1 + c2 = 7.2 + 5.3 i
c1 - c2 = -2.4 + 1.9 i
c1 * c2 = 5.4 + 21.36 i
c1 / c2 = 0.942308 + 0.705128 i

相關文章