![](/img/trans.png)
[英]Dynamic Array Template Class: problem with ostream& operator friend function
[英](ostream& outs) in class function parameters C++
類函數ostream& outs
中的參數是什么意思? 例如
void BankAccout::output(ostream& outs){
outs.setf(ios::fixed);
outs.setf(ios::showpoint);
outs.precision(2);
outs<<"account balance $"<<balance<<endl;
outs<<"Interest rate"<<interest_rate<<"%"<<endl;
}
為什么將信息輸出到輸出而不使用cout而是使用outs?
使自己熟悉流: http : //www.cplusplus.com/reference/iolibrary/
基本上,ostream是要寫入的流,將數據輸出到某個地方。 cout也是源源不斷。 但是您也可以將文件作為ostream打開。 因此,此功能使您可以決定將數據寫入何處。 如果要將數據寫入終端,則可以將cout作為參數傳遞。 如果要將其存儲在文件中,則可以將文件作為ostream打開,然后將其傳遞給函數。
例如:
int main(void)
{
BankAccount *ba = new BankAccount();
ba->output(cout); //prints to terminal
std::ofstream ofile; //ofstream is derived from ostream
ofile.open("test.txt");
ba->output(ofile); //will output to the file "test.txt"
delete ba;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.