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