cout and cin are not functions, so what are they?

荒凉一梦 提交于 2019-12-03 03:20:12

问题


Generally in C programming language We consider printf and scanf to be functions. when it comes to cout and cin, in C++ what are they?I mean they cant be functions as they are not followed by parenthesis,so they are not functions. So what are cout and cin are standard input and output functions?OR something else?


回答1:


std::cout and std::cin are global objects of classes std::ostream and std::istream respectively, which they've overloaded operator << and >>. You should read about operator overloading.

   cout    <<      expr  ;
  ~~~~~~  ~~~~   ~~~~~~~~
  object   op.   argument 

It's like a function call; the function is an overloaded operator and a shortcut for this:

cout.operator<<(expr);

or this:

operator<<(cout, expr);

depending on the results of overload resolution




回答2:


cout is object of type ostream. cin is object of type istream.




回答3:


They are global variables, declared in the header <iostream>




回答4:


Suppose x is an variable ( let integer type ) .We want to put value in it.What we do ?

We write cin>>x .

But we can also put the int data by writing cin.operator>>(x) . This implies cin is an object and operator >> is a function in which x is passed .



来源:https://stackoverflow.com/questions/20070606/cout-and-cin-are-not-functions-so-what-are-they

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!