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
類裏的私有成員函數。