[英](ostream& outs) in class function parameters C++
What does the parameter in a class function ostream& outs
mean? 类函数
ostream& outs
中的参数是什么意思? eg 例如
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;
}
and why is it that for outputing information onto ouput,it no longer uses cout,but now uses outs? 为什么将信息输出到输出而不使用cout而是使用outs?
Make yourself familiar with streams: http://www.cplusplus.com/reference/iolibrary/ 使自己熟悉流: http : //www.cplusplus.com/reference/iolibrary/
Basically ostreams are streams to write into, that output the data somewhere. 基本上,ostream是要写入的流,将数据输出到某个地方。 cout is also an ostream.
cout也是源源不断。 But you can also open a file as an ostream.
但是您也可以将文件作为ostream打开。 So this function lets you decide where the data should be written to.
因此,此功能使您可以决定将数据写入何处。 If you want the data written in the terminal you pass cout as the argument.
如果要将数据写入终端,则可以将cout作为参数传递。 If you want it in a file, you open a file as an ostream and pass that to the function instead.
如果要将其存储在文件中,则可以将文件作为ostream打开,然后将其传递给函数。
Just for example: 例如:
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.