C++ 流插入和流提取運算符的重載的實現(xiàn)
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
04 小結(jié)
要想流插入 << 運算符和流提取 >> 運算符能針對自定義的對象,那么我們就需要重載針對該對象的 ostream 類的 << 運算符 和 istream 的 >> 運算符,并且只能重載成全局的函數(shù),然后在 CStudent 類里需要把上面的兩個重載函數(shù)聲明成友元函數(shù),使得兩個重載的函數(shù)可以訪問和賦值 CStudent 類里的私有成員函數(shù)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
下一篇:C語言實現(xiàn)食堂就餐管理系統(tǒng)(帶鏈表)
本文標題:C++ 流插入和流提取運算符的重載的實現(xiàn)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/108.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內(nèi)存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置