C++重載輸入和輸出運算符詳解
在C++中,標準庫本身已經(jīng)對左移運算符<<
和右移運算符>>
分別進行了重載,使其能夠用于不同數(shù)據(jù)的輸入輸出,但是輸入輸出的對象只能是 C++ 內(nèi)置的數(shù)據(jù)類型(例如 bool、int、double 等)和標準庫所包含的類類型(例如 string、complex、ofstream、ifstream 等)。
如果我們自己定義了一種新的數(shù)據(jù)類型,需要用輸入輸出運算符去處理,那么就必須對它們進行重載。本節(jié)以前面的 complex 類為例來演示輸入輸出運算符的重載。
其實 C++ 標準庫已經(jīng)提供了 complex 類,能夠很好地支持復(fù)數(shù)運算,但是這里我們又自己定義了一個 complex 類,這樣做僅僅是為了教學(xué)演示。
本節(jié)要達到的目標是讓復(fù)數(shù)的輸入輸出和 int、float 等基本類型一樣簡單。假設(shè) num1、num2 是復(fù)數(shù),那么輸出形式就是:
cout<<num1<<num2<<endl;
輸入形式就是:
cin>>num1>>num2;
cout 是 ostream 類的對象,cin 是 istream 類的對象,要想達到這個目標,就必須以全局函數(shù)(友元函數(shù))的形式重載<<
和>>
,否則就要修改標準庫中的類,這顯然不是我們所期望的。
重載輸入運算符>>
下面我們以全局函數(shù)的形式重載>>
,使它能夠讀入兩個 double 類型的數(shù)據(jù),并分別賦值給復(fù)數(shù)的實部和虛部:
istream & operator>>(istream &in, complex &A){ in >> A.m_real >> A.m_imag; return in; }
istream 表示輸入流,cin 是 istream 類的對象,只不過這個對象是在標準庫中定義的。之所以返回 istream 類對象的引用,是為了能夠連續(xù)讀取復(fù)數(shù),讓代碼書寫更加漂亮,例如:
complex c1, c2; cin>>c1>>c2;
如果不返回引用,那就只能一個一個地讀取了:
complex c1, c2; cin>>c1; cin>>c2;
另外,運算符重載函數(shù)中用到了 complex 類的 private 成員變量,必須在 complex 類中將該函數(shù)聲明為友元函數(shù):
friend istream & operator>>(istream & in , complex &a);
>>
運算符可以按照下面的方式使用:
complex c; cin>>c;
當(dāng)輸入1.45 2.34↙
后,這兩個小數(shù)就分別成為對象 c 的實部和虛部了。cin>> c;
這一語句其實可以理解為:
operator<<(cin , c);
重載輸出運算符<<
同樣地,我們也可以模仿上面的形式對輸出運算符>>
進行重載,讓它能夠輸出復(fù)數(shù),請看下面的代碼:
ostream & operator<<(ostream &out, complex &A){ out << A.m_real <<" + "<< A.m_imag <<" i "; return out; }
ostream 表示輸出流,cout 是 ostream 類的對象。由于采用了引用的方式進行參數(shù)傳遞,并且也返回了對象的引用,所以重載后的運算符可以實現(xiàn)連續(xù)輸出。為了能夠直接訪問 complex 類的 private 成員變量,同樣需要將該函數(shù)聲明為 complex 類的友元函數(shù):
friend ostream & operator<<(ostream &out, complex &A);
綜合演示
結(jié)合輸入輸出運算符的重載,重新實現(xiàn) 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; }
運行結(jié)果:
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++重載輸入和輸出運算符詳解的詳細內(nèi)容,更多關(guān)于C++重載輸入和輸出運算符的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)
下面小編就為大家?guī)硪黄獪\談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03C++ 使用CRC32檢測內(nèi)存映像完整性的實現(xiàn)步驟
當(dāng)我們使用動態(tài)補丁的時候,那么內(nèi)存中同樣不存在校驗效果,也就無法抵御對方動態(tài)修改機器碼了,為了防止解密者直接對內(nèi)存打補丁,我們需要在硬盤校驗的基礎(chǔ)上,增加內(nèi)存校驗,防止動態(tài)補丁的運用。2021-06-06C語言報錯Use of Uninitialized Variable的原因及解決方案
Use of Uninitialized Variable是C語言中常見且危險的錯誤之一,它通常在程序試圖使用一個未初始化的變量時發(fā)生,本文將詳細介紹Use of Uninitialized Variable的產(chǎn)生原因,提供多種解決方案,并通過實例代碼演示如何有效避免和解決此類錯誤,需要的朋友可以參考下2024-06-06詳解C++中的內(nèi)存同步模式(memory order)
這篇文章主要介紹了C++中的內(nèi)存同步模式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04利用C語言模擬實現(xiàn)qsort,strcpy,strcat,strcmp函數(shù)
這篇文章主要為大家詳細介紹了如何通過C語言模擬實現(xiàn)qsort(采用冒泡的方式),strcpy,strcat,strcmp等函數(shù),文中的示例代碼講解詳細,感興趣的可以了解一下2022-11-11使用C++模擬實現(xiàn)2024春晚劉謙魔術(shù)
劉謙在2024年春晚上的撕牌魔術(shù)的數(shù)學(xué)原理非常簡單,所以這篇文章主要為大家詳細介紹了如何使用C++模擬實現(xiàn)這一魔術(shù)效果,感興趣的可以了解下2024-02-02