怎么在C++中實現(xiàn)流插入和流提取運算符的重載?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

01 流插入<<運算符的重載
C++ 在輸出內(nèi)容時,最常用的方式:
std::cout << 1 <<"hello";
問題:
那這條語句為什么能成立呢?
cout 是什么?"<<" 運算符能用在 cout 上呢?
原因:
實際上,cout 是在 iostream 頭文件中定義的 ostream 類的對象。
"<<" 能夠用在 cout 上是因為,在 ostream 類對 "<<" 進行了重載。
對于std::cout << 1 <<"hello";這條語句,有可能按以下的方式重載成 ostream 類的成員函數(shù):
ostream & ostream::operator<<(int n)
{
.... // 輸出n整型的代碼
return *this;
}
ostream & ostream::operator<<(const char * s)
{
.... // 輸出s字符串的代碼
return *this;
}std::cout << 1;語句,等價于cout.operator<<(1);
std::cout << "hello";語句,等價于cout.operator<<("hello");
std::cout << 1 <<"hello";語句,等價于( cout.operator<<(1) ).operator<<("hello");
02 流插入<<運算符重載的例子
假定我們要想把某個對象里的內(nèi)容進行打印輸出,那么我們可以重載 ostream 類的流插入 << 運算符。
下面以 CStudent 類作為例子:
class CStudent // 學(xué)生類
{
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ù)可以訪問CStudent類的私有成員變量
friend ostream & operator<<(ostream & o, const CStudent & s);
private:
int m_age; // 年齡
int m_id; // ID號
string m_name; // 名字
};
// 重載ostream對象的流插入<<運算符函數(shù)
// 目的是使得能打印輸出CStudent對象的信息
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對象的全部信息
return 0;
}輸出結(jié)果:
1,20,小林coding
需要注意是 ostream & operator<<(ostream & o, const CStudent & s) 函數(shù)是全局的,所以函數(shù)的第一個參數(shù)必須要傳入 ostream 的對象,并且 CStudent 類需要將此函數(shù)聲明成友元函數(shù),使得函數(shù)可以訪問 CStudent 類的私有成員變量。
03 流提取>>運算符重載的例子
還是以 CStudent 類作為例子,假設(shè)想通過鍵盤的輸入的內(nèi)容,來初始化對象,則我們可以重載 istream 類的流提取 >> 運算符。
class CStudent // 學(xué)生類
{
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ù)可以訪問CStudent類的私有成員變量
friend ostream & operator<<(ostream & o, const CStudent & s);
// 將該函數(shù)聲明成友元函數(shù)
// 目的是使得函數(shù)可以給CStudent類的私有成員變量進行賦值
friend istream & operator>>(istream & is, CStudent & s);
private:
int m_age; // 年齡
int m_id; // ID號
string m_name; // 名字
};
// 重載ostream對象的流插入<<運算符函數(shù)
// 目的是使得能打印輸出CStudent對象的信息
ostream & operator<<(ostream & o, const CStudent & s)
{
o << s.m_id << "," << s.m_age << "," << s.m_name;
return o;
}
// 重載istream對象的流提取>>運算符函數(shù)
// 目的是使得初始化CStudent對象的內(nèi)容
istream & operator>>(istream & is, CStudent & stu)
{
string inputStr;
is >> inputStr;
int pos = inputStr.find(",", 0); // 查找首次出現(xiàn)逗號的位置
string tmpStr = inputStr.substr(0, pos); // 截取從0到pos位置的字符串
stu.id = atoi(tmpStr.c_str()); // atoi可以將char*類型的內(nèi)容轉(zhuǎn)成int類型
int pos2 = inputStr.find(",", pos + 1); // 查找第二次出現(xiàn)逗號的位置
tmpStr = inputStr.substr(pos + 1, pos2 - pos -1); // 取出age的值
stu.age = atoi(tmpStr.c_str()); // atoi可以將char*類型的內(nèi)容轉(zhuǎn)成int類型
tmpStr = inputStr.substr(pos2 + 1, inputStr.length() - pos2 - 1); // 取出name的值
stu.name = tmpStr;
return is;
}
int main()
{
CStudent stu;
// 將輸入的信息,初始化stu對象
cin << stu;
// 輸出std對象的信息
cout >> stu;
return 0;
}輸入內(nèi)容和輸出內(nèi)容:
// 輸入內(nèi)容:
1,20,小林coding// 輸出內(nèi)容:
1,20,小林coding
關(guān)于怎么在C++中實現(xiàn)流插入和流提取運算符的重載問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
名稱欄目:怎么在C++中實現(xiàn)流插入和流提取運算符的重載-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article42/hhdhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、手機網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、軟件開發(fā)、Google、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容