微信公眾號:「小林coding」
用簡潔的方式,分享編程小知識,
01 流插入<<運算子的多載
C++ 在輸出內容時,最常用的方式:
std::cout << 1 <<"hello";
問題:
- 那這條陳述句為什么能成立呢?
cout是什么?"<<" 運算子能用在cout上呢?
原因:
- 實際上,
cout是在iostream頭檔案中定義的ostream類的物件, - "
<<" 能夠用在cout上是因為,在ostream類對 "<<" 進行了多載,
對于std::cout << 1 <<"hello";這條陳述句,有可能按以下的方式多載成 ostream 類的成員函式:
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 流插入<<運算子多載的例子
假定我們要想把某個物件里的內容進行列印輸出,那么我們可以多載 ostream 類的流插入 << 運算子,
下面以 CStudent 類作為例子:
class CStudent // 學生類
{
public:
// 建構式
CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
// 將該函式宣告成友元函式
// 目的是使得函式可以訪問CStudent類的私有成員變數
friend ostream & operator<<(ostream & o, const CStudent & s);
private:
int m_age; // 年齡
int m_id; // ID號
string m_name; // 名字
};
// 多載ostream物件的流插入<<運算子函式
// 目的是使得能列印輸出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;
}
輸出結果:
1,20,小林coding
需要注意是 ostream & operator<<(ostream & o, const CStudent & s) 函式是全域的,所以函式的第一個引數必須要傳入 ostream 的物件,并且 CStudent 類需要將此函式宣告成友元函式,使得函式可以訪問 CStudent 類的私有成員變數,
03 流提取>>運算子多載的例子
還是以 CStudent 類作為例子,假設想通過鍵盤的輸入的內容,來初始化物件,則我們可以多載 istream 類的流提取 >> 運算子,
class CStudent // 學生類
{
public:
// 建構式
CStudent(int id = 0, int age = 0, string name = ""):m_id(id), m_age(age), m_name(name) { }
// 將該函式宣告成友元函式
// 目的是使得函式可以訪問CStudent類的私有成員變數
friend ostream & operator<<(ostream & o, const CStudent & s);
// 將該函式宣告成友元函式
// 目的是使得函式可以給CStudent類的私有成員變數進行賦值
friend istream & operator>>(istream & is, CStudent & s);
private:
int m_age; // 年齡
int m_id; // ID號
string m_name; // 名字
};
// 多載ostream物件的流插入<<運算子函式
// 目的是使得能列印輸出CStudent物件的資訊
ostream & operator<<(ostream & o, const CStudent & s)
{
o << s.m_id << "," << s.m_age << "," << s.m_name;
return o;
}
// 多載istream物件的流提取>>運算子函式
// 目的是使得初始化CStudent物件的內容
istream & operator>>(istream & is, CStudent & stu)
{
string inputStr;
is >> inputStr;
int pos = inputStr.find(",", 0); // 查找首次出現逗號的位置
string tmpStr = inputStr.substr(0, pos); // 截取從0到pos位置的字串
stu.id = atoi(tmpStr.c_str()); // atoi可以將char*型別的內容轉成int型別
int pos2 = inputStr.find(",", pos + 1); // 查找第二次出現逗號的位置
tmpStr = inputStr.substr(pos + 1, pos2 - pos -1); // 取出age的值
stu.age = atoi(tmpStr.c_str()); // atoi可以將char*型別的內容轉成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;
}
輸入內容和輸出內容:
// 輸入內容:
1,20,小林coding
// 輸出內容:
1,20,小林coding
04 小結
要想流插入 << 運算子和流提取 >> 運算子能針對自定義的物件,那么我們就需要多載針對該物件的 ostream 類的 << 運算子 和 istream 的 >> 運算子,并且只能多載成全域的函式,然后在 CStudent 類里需要把上面的兩個多載函式宣告成友元函式,使得兩個多載的函式可以訪問和賦值 CStudent 類里的私有成員函式,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/83172.html
標籤:C++
上一篇:C++ 基礎語法 快速復習筆記(3)---多載函式,多型,虛函式
下一篇:day01

