Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

iostream

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?

like image 767
user2643191 Avatar asked Nov 19 '13 11:11

user2643191


1 Answers

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

like image 106
masoud Avatar answered Oct 31 '22 09:10

masoud