Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   The "<<" operator behind cout (http://www.velocityreviews.com/forums/t459721-the-operator-behind-cout.html)

Prateek 01-08-2007 04:21 PM

The "<<" operator behind cout
 
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 01-08-2007 04:27 PM

Re: The "<<" operator behind cout
 
"Prateek" <prateek.jain2@gmail.com> wrote in message
news:1168273286.055398.197940@s34g2000cwa.googlegr 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* )



Sylvester Hesp 01-08-2007 04:38 PM

Re: The "<<" operator behind cout
 
"Prateek" <prateek.jain2@gmail.com> wrote in message
news:1168273286.055398.197940@s34g2000cwa.googlegr 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::ostream, the operator<< is used to output a textual representation of
the right hand side argument to the stream.

- Sylvester



Tim Slattery 01-08-2007 05:39 PM

Re: The "<<" operator behind cout
 
"Prateek" <prateek.jain2@gmail.com> 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::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
Slattery_T@bls.gov
http://members.cox.net/slatteryt

prabhu_anic 01-08-2007 05:46 PM

Re: The "<<" operator behind cout
 

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.


prabhu_anic 01-08-2007 05:48 PM

Re: The "<<" operator behind cout
 

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.


Tim Slattery 01-08-2007 09:11 PM

Re: The "<<" operator behind cout
 
"prabhu_anic" <prabhu.anic@gmail.com> 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::ostream overloads this one (and others).

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

Mark P 01-08-2007 09:45 PM

Re: The "<<" operator behind cout
 
Tim Slattery wrote:
> "prabhu_anic" <prabhu.anic@gmail.com> 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::ostream overloads this one (and others).
>


*Almost* any...

http://www.parashift.com/c++-faq-lit....html#faq-13.5

Prateek Jain 01-12-2007 04:33 PM

Re: The "<<" operator behind cout
 
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!!!



All times are GMT. The time now is 12:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.