Discussion:
The "<<" operator behind cout
(too old to reply)
Prateek
2007-01-08 16:21:26 UTC
Permalink
Hey...
Can anybody tell me why "<<" operator is used behind cout?
Like:
cout<<"Welcome";
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
Jim Langston
2007-01-08 16:27:40 UTC
Permalink
Post by Prateek
Hey...
Can anybody tell me why "<<" operator is used behind cout?
cout<<"Welcome";
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
Unique to C++ (isnt' used that way in C).
It is the operator<<
That's the only thing I can see it's called as. It would call the function
prototyped something like (this may be wrong in details, probably is)
ostream& operator<<( ostream& os, const char* )
Sylvester Hesp
2007-01-08 16:38:32 UTC
Permalink
Post by Prateek
Hey...
Can anybody tell me why "<<" operator is used behind cout?
cout<<"Welcome";
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
The default operator<< for integers will shift the bits, yes. But in C++
most operators are overloadable, which means anyone can introduce his own
meaning to the various operators when used on custom types. For
std::ostream, the operator<< is used to output a textual representation of
the right hand side argument to the stream.

- Sylvester
prabhu_anic
2007-01-08 17:46:34 UTC
Permalink
Post by Prateek
Hey...
Can anybody tell me why "<<" operator is used behind cout?
cout<<"Welcome";
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.
Tim Slattery
2007-01-08 21:11:21 UTC
Permalink
Post by prabhu_anic
In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.
In C++ the "<<" operator is also bitwise shift. *Any* operator can be
overloaded in C_++, std::ostream overloads this one (and others).

--
Tim Slattery
***@bls.gov
http://members.cox.net/slatteryt
Mark P
2007-01-08 21:45:28 UTC
Permalink
Post by Tim Slattery
Post by prabhu_anic
In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.
In C++ the "<<" operator is also bitwise shift. *Any* operator can be
overloaded in C_++, std::ostream overloads this one (and others).
*Almost* any...

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.5
Prateek Jain
2007-01-12 16:33:25 UTC
Permalink
Thanks to you all. I also asked this question to my school C++ teacher.
She said that "<<" take the thing behind bitwise(bit by bit, one bit at
a time), and that's why it is said that "it shifts the bits". According
to her, in cin>>, ">>" is also the same but it takes input. But I was
not satisfied by this answer. But after reading your replies, now I
think I'm quite clear about it. Thank You!!!

prabhu_anic
2007-01-08 17:48:14 UTC
Permalink
Post by Prateek
Hey...
Can anybody tell me why "<<" operator is used behind cout?
cout<<"Welcome";
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.
Tim Slattery
2007-01-08 17:39:00 UTC
Permalink
Post by Prateek
Hey...
Can anybody tell me why "<<" operator is used behind cout?
cout<<"Welcome";
My one friend told me that it "shifts the bits"...But it's not very
clear to me...Can anybody tell?
The "<<" operator normally shifts bits. But cout is of type
std::ostream, and that class overloads the "<<" operator, redefining
it as "insertion". The item on the right side of the operator
("Welcome", in your example) is inserted into the ostream. That causes
it to be written to whatever the ostream has been told to write to.
For the standard object "cout" that's the console.

--
Tim Slattery
***@bls.gov
http://members.cox.net/slatteryt
Loading...