![]() |
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? |
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* ) |
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 |
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 |
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. |
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. |
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 |
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 |
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.