C++重載>>和<<(輸入輸出運算符)
在C++中,標準庫本身已經對左移運算符<<
和右移運算符>>
分別進行了重載,使其能夠用於不同數據的輸入輸出,但是輸入輸出的對象只能是 C++ 內置的數據類型(例如 bool、int、double 等)和標準庫所包含的類類型(例如 string、complex、ofstream、ifstream 等)。
如果我們自己定義了一種新的數據類型,需要用輸入輸出運算符去處理,那麽就必須對它們進行重載。本節以前面的 complex 類為例來演示輸入輸出運算符的重載。
重載輸入運算符>>
下面我們以全局函數的形式重載>>
,使它能夠讀入兩個 double 類型的數據,並分別賦值給復數的實部和虛部:
istream & operator>>(istream &in, complex &A){ in >> A.m_real >> A.m_imag; return in; }
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);
重載輸出運算符<<
同樣地,我們也可以模仿上面的形式對輸出運算符>>
進行重載,讓它能夠輸出復數,請看下面的代碼:
ostream & operator<<(ostream &out, complex &A){ out << A.m_real <<"+ "<< A.m_imag <<" i "; return out; }
ostream 表示輸出流,cout 是 ostream 類的對象。由於采用了引用的方式進行參數傳遞,並且也返回了對象的引用,所以重載後的運算符可以實現連續輸出。
為了能夠直接訪問 complex 類的 private 成員變量,同樣需要將該函數聲明為 complex 類的友元函數:
(可以加上const修飾)
friend ostream & operator<<(ostream &out, const complex &A);
綜合演示
結合輸入輸出運算符的重載,重新實現 complex 類:
#include <iostream> using namespace std; class complex{ public: complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ }; public: friend complex operator+(const complex & A, const complex & B); friend complex operator-(const complex & A, const complex & B); friend complex operator*(const complex & A, const complex & B); friend complex operator/(const complex & A, const complex & B); friend istream & operator>>(istream & in, complex & A); friend ostream & operator<<(ostream & out, complex & A); private: double m_real; //實部 double m_imag; //虛部 }; //重載加法運算符 complex operator+(const complex & A, const complex &B){ complex C; C.m_real = A.m_real + B.m_real; C.m_imag = A.m_imag + B.m_imag; return C; } //重載減法運算符 complex operator-(const complex & A, const complex &B){ complex C; C.m_real = A.m_real - B.m_real; C.m_imag = A.m_imag - B.m_imag; return C; } //重載乘法運算符 complex operator*(const complex & A, const complex &B){ complex C; C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag; C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag; return C; } //重載除法運算符 complex operator/(const complex & A, const complex & B){ complex C; double square = A.m_real * A.m_real + A.m_imag * A.m_imag; C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square; C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square; return C; } //重載輸入運算符 istream & operator>>(istream & in, complex & A){ in >> A.m_real >> A.m_imag; return in; } //重載輸出運算符 ostream & operator<<(ostream & out, complex & A){ out << A.m_real <<" + "<< A.m_imag <<" i ";; return out; } int main(){ complex c1, c2, c3; cin>>c1>>c2; c3 = c1 + c2; cout<<"c1 + c2 = "<<c3<<endl; c3 = c1 - c2; cout<<"c1 - c2 = "<<c3<<endl; c3 = c1 * c2; cout<<"c1 * c2 = "<<c3<<endl; c3 = c1 / c2; cout<<"c1 / c2 = "<<c3<<endl; return 0; }
運行結果:
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
C++重載>>和<<(輸入輸出運算符)