cout and cin are not functions, so what are they?
Asked Answered
A

4

18

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?

Arcadian answered 19/11, 2013 at 11:42 Comment(3)
Questions like this one are so very basic that you should be able to look them up in any C++ textbook and even in most tutorials covering the topic.Recount
You might want to check out The Definitive C++ Book Guide and List for beginners books and tutorials. Also if you want an online reference, this one is considered pretty good.Mortenson
I don't see anything wrong with this question. He's not asking what they do.Gillies
T
20

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

Trichina answered 19/11, 2013 at 11:43 Comment(1)
Can you please explain what did you mean by "depending on the results of overload resolution"? As overload resolutions happen for a different type of the arguments of a function. Isn't the arguments same here? Can you write how the operator overload definition might look like in both these separate cases. Are you suggesting that for the second case, << might not be a member of ostream class?Stacked
W
8

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

Womanhood answered 19/11, 2013 at 11:42 Comment(2)
so ostream and istream are classes?Arcadian
@user2643191: Exactly. :)Gillies
J
3

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

Jochbed answered 19/11, 2013 at 11:44 Comment(0)
M
-1

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 .

Melchor answered 24/5, 2018 at 12:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.