简体   繁体   English

类函数参数C ++中的(ostream&outs)

[英](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.

相关问题 动态数组模板类:ostream和操作员好友功能问题 - Dynamic Array Template Class: problem with ostream& operator friend function C++ ostream 参数 - C++ ostream parameters C ++在类头中声明带有ostream的函数 - c++ declaring a function with ostream in class header 未定义对 `operator&lt;&lt;(std::ostream&amp;, /* class 与非类型模板参数 */&amp;)' 的引用 - undefined reference to `operator<<(std::ostream&, /* class with non-type template parameters */&)' operator &lt;&lt;(ostream&os,...)用于模板类 - operator<<(ostream& os, …) for template class 拥有一个具有写入 function 的 class,如何将其传递给接受 std::ostream&amp; 的 function - Having a class that has a write function, how to I pass it to a function accepting std::ostream& “对&#39;std::operator&lt;&lt;(std::ostream&amp;, std::LinkedList const&amp;)的未定义引用”C++ - "Undefined reference to 'std::operator<<(std::ostream&, std::LinkedList const&)" C++ 重载&lt;&lt;在C ++中的运算符-为什么返回类型是引用std :: ostream& - Overloading the << operator in C++ -why return type is a reference std::ostream& 为什么必须在 C++ 的“全局”scope 中声明 `std::ostream&amp; operator&lt;&lt;` 覆盖? - Why `std::ostream& operator<<` overriding must be declare in 'global' scope in C++? 有关初始化类型为“ std :: ostream”的临时类的非常量引用“ std :: ostream&”的C ++编译器错误 - C++ compiler error regarding initialization of a non-const reference of type 'std::ostream&' from a temporary of type 'std::ostream"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM