The "<<" operator behind cout

P

Prateek

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?
 
J

Jim Langston

Prateek said:
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?

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* )
 
S

Sylvester Hesp

Prateek said:
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?

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::eek:stream, the operator<< is used to output a textual representation of
the right hand side argument to the stream.

- Sylvester
 
T

Tim Slattery

Prateek said:
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?

The "<<" operator normally shifts bits. But cout is of type
std::eek:stream, 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
(e-mail address removed)
http://members.cox.net/slatteryt
 
P

prabhu_anic

Prateek said:
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?

In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.
 
P

prabhu_anic

Prateek said:
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?

In C the operator << is a bitwise shift operator.
But in C++ this operator is an overloaded operator that works as an
output operator.
 
T

Tim Slattery

prabhu_anic said:
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::eek:stream overloads this one (and others).

--
Tim Slattery
(e-mail address removed)
http://members.cox.net/slatteryt
 
P

Prateek Jain

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!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top