user5182503
user5182503

Reputation:

What exactly are cout/cin?

I know Java and now want to learn C++. I can't understand what are cout (character output stream) and cin (character input). Are these global variables? Then why

"My message">>cout; 

doesn't work? But

cout<<"My message";

works.

Upvotes: 16

Views: 9084

Answers (4)

xAditya3393
xAditya3393

Reputation: 149

iostream is a header file which contain classes handling input and output operations for a console. Its like you create a object when you say "cin" for the input class handling input operation for a console in the header file. Same can be said about "cout" where a object is being created from a class handling output operation to a console in the header file.

When you consider "cin", imagine creating a pipe connected to the console and your program and an object "cin" taking your inputs from the console which you provide through your keyboard and dumping them on to the program. That's the reason you can see having a ">>" operator for cin and you can find the analogy to the pipe where the operator is taking the job of specifying the direction for the object "cin".

Whereas for "cout", imagine creating a pipe connected to the console and your program and an object "cout" taking its input from the program and dumping them on to the console. That's the reason you can see having a "<<" operator for cout and you find the analogy to the pipe where the operator is taking the job of specifying the direction for the object "cout".

So basically you need to first specify what object you would be creating for your operations and then assigning an operator to accomplish your task. If you include the header file, then its like you could use those objects anywhere throughout your program.

So, "My message">>cout; doesn't function the way you expect it to be because there is no object and an operator to accomplish your task whereas cout<<"My message"; does.

The technical aspects have been described by Mats Petersson. This is just to give you a general picture of what's actually happening pictorially. Hope this helps you.

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129314

cout is an instance of the class std::ostream, and yes, it's a global variable. But operator>>(char *, ostream& os); hasn't been declared by the relevant header, so "My message">>cout; will give an error of something like "can't find an operator >> which takes arguments const char * and std::ostream" (and possibly a lot more errors because sometimes compilers get very confused by these sort of things).

cin is the same thing, except std::istream

If you really want to mess with peoples heads, you could do:

template<typename T>
std::ostream& operator>>(T x, std::ostream& os)
{
   os << x;
   return os;
}

Of course, it won't work for "My Message " >> "Some other string" >> cout;, which is probably one of the reasons it's not done that way.

Note that this is simply slight abuse of the operator overloading, where we have a custom type as the left-hand side, and standard or non-standard type on the right hand side. cout is no different from some other variable of a custom type.

Upvotes: 27

std::cout and std::cin are indeed global variables. Your code doesn't compile because that's not the way the language works. You have to put the stream on the left, and then the operator and then the variables you are streaming into/out of. (For output, you can use literals and expressions as well as variables.)

Upvotes: 5

George Moralis
George Moralis

Reputation: 516

consider the arrows as streams. << stands for output stream , while >> stands for input stream.

so cout << "hello" means output to screen when cin >> a means asks from a user input for variable a

cout can also use "+" like for example you can add more strings to one stream like this

cout << "Hello" << "world" << "I am john";

cin in the same way can ask for input from multiple variables

cin >> a >> b ; will ask from user to input two times one for each variable

Upvotes: 0

Related Questions