C++ 流插入和流提取運(yùn)算符的重載的實(shí)現(xiàn)
01 流插入<<運(yùn)算符的重載
C++ 在輸出內(nèi)容時(shí),最常用的方式:
std::cout << 1 <<"hello";
問(wèn)題:
- 那這條語(yǔ)句為什么能成立呢?
- cout 是什么?"<<" 運(yùn)算符能用在 cout 上呢?
原因:
- 實(shí)際上,cout 是在 iostream 頭文件中定義的 ostream 類(lèi)的對(duì)象。
- "<<" 能夠用在 cout 上是因?yàn)?,?ostream 類(lèi)對(duì) "<<" 進(jìn)行了重載。
對(duì)于std::cout << 1 <<"hello";這條語(yǔ)句,有可能按以下的方式重載成 ostream 類(lèi)的成員函數(shù):
ostream & ostream::operator<<(int n) { .... // 輸出n整型的代碼 return *this; } ostream & ostream::operator<<(const char * s) { .... // 輸出s字符串的代碼 return *this; }
- std::cout << 1;語(yǔ)句,等價(jià)于cout.operator<<(1);
- std::cout << "hello";語(yǔ)句,等價(jià)于cout.operator<<("hello");
- std::cout << 1 <<"hello";語(yǔ)句,等價(jià)于( cout.operator<<(1) ).operator<<("hello");
02 流插入<<運(yùn)算符重載的例子
假定我們要想把某個(gè)對(duì)象里的內(nèi)容進(jìn)行打印輸出,那么我們可以重載 ostream 類(lèi)的流插入 << 運(yùn)算符。
下面以 CStudent 類(lèi)作為例子:
class CStudent // 學(xué)生類(lèi) { public: // 構(gòu)造函數(shù) CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { } // 將該函數(shù)聲明成友元函數(shù) // 目的是使得函數(shù)可以訪問(wèn)CStudent類(lèi)的私有成員變量 friend ostream & operator<<(ostream & o, const CStudent & s); private: int m_age; // 年齡 int m_id; // ID號(hào) string m_name; // 名字 }; // 重載ostream對(duì)象的流插入<<運(yùn)算符函數(shù) // 目的是使得能打印輸出CStudent對(duì)象的信息 ostream & operator<<(ostream & o, const CStudent & s) { o << s.m_id << "," << s.m_age << "," << s.m_name; return o; } int main() { CStudent stu(1, 20, "小林coding"); std::cout << stu ; // 輸出std對(duì)象的全部信息 return 0; }
輸出結(jié)果:
1,20,小林coding
需要注意是 ostream & operator<<(ostream & o, const CStudent & s) 函數(shù)是全局的,所以函數(shù)的第一個(gè)參數(shù)必須要傳入 ostream 的對(duì)象,并且 CStudent 類(lèi)需要將此函數(shù)聲明成友元函數(shù),使得函數(shù)可以訪問(wèn) CStudent 類(lèi)的私有成員變量。
03 流提取>>運(yùn)算符重載的例子
還是以 CStudent 類(lèi)作為例子,假設(shè)想通過(guò)鍵盤(pán)的輸入的內(nèi)容,來(lái)初始化對(duì)象,則我們可以重載 istream 類(lèi)的流提取 >> 運(yùn)算符。
class CStudent // 學(xué)生類(lèi) { public: // 構(gòu)造函數(shù) CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { } // 將該函數(shù)聲明成友元函數(shù) // 目的是使得函數(shù)可以訪問(wèn)CStudent類(lèi)的私有成員變量 friend ostream & operator<<(ostream & o, const CStudent & s); // 將該函數(shù)聲明成友元函數(shù) // 目的是使得函數(shù)可以給CStudent類(lèi)的私有成員變量進(jìn)行賦值 friend istream & operator>>(istream & is, CStudent & s); private: int m_age; // 年齡 int m_id; // ID號(hào) string m_name; // 名字 }; // 重載ostream對(duì)象的流插入<<運(yùn)算符函數(shù) // 目的是使得能打印輸出CStudent對(duì)象的信息 ostream & operator<<(ostream & o, const CStudent & s) { o << s.m_id << "," << s.m_age << "," << s.m_name; return o; } // 重載istream對(duì)象的流提取>>運(yùn)算符函數(shù) // 目的是使得初始化CStudent對(duì)象的內(nèi)容 istream & operator>>(istream & is, CStudent & stu) { string inputStr; is >> inputStr; int pos = inputStr.find(",", 0); // 查找首次出現(xiàn)逗號(hào)的位置 string tmpStr = inputStr.substr(0, pos); // 截取從0到pos位置的字符串 stu.id = atoi(tmpStr.c_str()); // atoi可以將char*類(lèi)型的內(nèi)容轉(zhuǎn)成int類(lèi)型 int pos2 = inputStr.find(",", pos + 1); // 查找第二次出現(xiàn)逗號(hào)的位置 tmpStr = inputStr.substr(pos + 1, pos2 - pos -1); // 取出age的值 stu.age = atoi(tmpStr.c_str()); // atoi可以將char*類(lèi)型的內(nèi)容轉(zhuǎn)成int類(lèi)型 tmpStr = inputStr.substr(pos2 + 1, inputStr.length() - pos2 - 1); // 取出name的值 stu.name = tmpStr; return is; } int main() { CStudent stu; // 將輸入的信息,初始化stu對(duì)象 cin << stu; // 輸出std對(duì)象的信息 cout >> stu; return 0; }
輸入內(nèi)容和輸出內(nèi)容:
// 輸入內(nèi)容:
1,20,小林coding// 輸出內(nèi)容:
1,20,小林coding
04 小結(jié)
要想流插入 << 運(yùn)算符和流提取 >> 運(yùn)算符能針對(duì)自定義的對(duì)象,那么我們就需要重載針對(duì)該對(duì)象的 ostream 類(lèi)的 << 運(yùn)算符 和 istream 的 >> 運(yùn)算符,并且只能重載成全局的函數(shù),然后在 CStudent 類(lèi)里需要把上面的兩個(gè)重載函數(shù)聲明成友元函數(shù),使得兩個(gè)重載的函數(shù)可以訪問(wèn)和賦值 CStudent 類(lèi)里的私有成員函數(shù)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言中access/_access函數(shù)的使用實(shí)例詳解
本文通過(guò)實(shí)例代碼給大家介紹了C語(yǔ)言中access/_access函數(shù)的使用,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09C++ 輕量級(jí)對(duì)象JSON序列化實(shí)現(xiàn)詳情
本文以jsoncpp庫(kù)為基礎(chǔ),設(shè)計(jì)這樣一個(gè)可以支持一個(gè)函數(shù) 可以一行代碼 unmarshal /marshal 對(duì)象,需要的朋友小伙伴可以參考以下2021-09-09Qt實(shí)現(xiàn)TCP網(wǎng)絡(luò)編程
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)TCP網(wǎng)絡(luò)編程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08常用的C++標(biāo)準(zhǔn)庫(kù)頭文件小結(jié)
C++標(biāo)準(zhǔn)庫(kù)定義了一系列函數(shù)、宏和對(duì)象,以實(shí)現(xiàn)跨團(tuán)隊(duì)、跨平臺(tái)的高效且具有卓越性能的標(biāo)準(zhǔn)化 C++ 代碼, 本文介紹常用的C++標(biāo)準(zhǔn)庫(kù)頭文件,需要的朋友可以參考下2023-11-11C語(yǔ)言 超詳細(xì)介紹與實(shí)現(xiàn)線性表中的帶頭雙向循環(huán)鏈表
帶頭雙向循環(huán)鏈表:結(jié)構(gòu)最復(fù)雜,一般用在單獨(dú)存儲(chǔ)數(shù)據(jù)。實(shí)際中使用的鏈表數(shù)據(jù)結(jié)構(gòu),都是帶頭雙向循環(huán)鏈表。另外這個(gè)結(jié)構(gòu)雖然結(jié)構(gòu)復(fù)雜,但是使用代碼實(shí)現(xiàn)以后會(huì)發(fā)現(xiàn)結(jié)構(gòu)會(huì)帶來(lái)很多優(yōu)勢(shì),實(shí)現(xiàn)反而簡(jiǎn)單2022-03-03c++打印封裝每次打印前面加上時(shí)間戳問(wèn)題
這篇文章主要介紹了c++打印封裝每次打印前面加上時(shí)間戳問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01