Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > The "<<" operator behind cout

Reply
Thread Tools

The "<<" operator behind cout

 
 
Prateek
Guest
Posts: n/a
 
      01-08-2007
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?

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      01-08-2007
"Prateek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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* )


 
Reply With Quote
 
 
 
 
Sylvester Hesp
Guest
Posts: n/a
 
      01-08-2007
"Prateek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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:stream, the operator<< is used to output a textual representation of
the right hand side argument to the stream.

- Sylvester


 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      01-08-2007
"Prateek" <(E-Mail Removed)> wrote:

>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: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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
prabhu_anic
Guest
Posts: n/a
 
      01-08-2007

Prateek wrote:
> 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.

 
Reply With Quote
 
prabhu_anic
Guest
Posts: n/a
 
      01-08-2007

Prateek wrote:
> 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.

 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      01-08-2007
"prabhu_anic" <(E-Mail Removed)> wrote:

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

--
Tim Slattery
(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      01-08-2007
Tim Slattery wrote:
> "prabhu_anic" <(E-Mail Removed)> wrote:
>
>> 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:stream overloads this one (and others).
>


*Almost* any...

http://www.parashift.com/c++-faq-lit....html#faq-13.5
 
Reply With Quote
 
Prateek Jain
Guest
Posts: n/a
 
      01-12-2007
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!!!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: cout vs std::cout Stefan Ram C++ 7 09-30-2008 01:55 PM
error: no match for ‘operator<<’ in ‘std::cout.std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](k) << dispMyarr(k)’ curiousEngine C++ 1 05-09-2008 09:34 AM
can cout<<""; can cout<<""; contribute ? wangzhihuii@yahoo.com.cn C++ 4 09-15-2005 06:16 PM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments