The "<<" operator behind cout

Discussion in 'C++' started by Prateek, Jan 8, 2007.

  1. Prateek

    Prateek Guest

    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?
     
    Prateek, Jan 8, 2007
    #1
    1. Advertising

  2. Prateek

    Jim Langston Guest

    "Prateek" <> wrote in message
    news:...
    > 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* )
     
    Jim Langston, Jan 8, 2007
    #2
    1. Advertising

  3. "Prateek" <> wrote in message
    news:...
    > 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
     
    Sylvester Hesp, Jan 8, 2007
    #3
  4. Prateek

    Tim Slattery Guest

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


    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

    http://members.cox.net/slatteryt
     
    Tim Slattery, Jan 8, 2007
    #4
  5. Prateek

    prabhu_anic Guest

    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, Jan 8, 2007
    #5
  6. Prateek

    prabhu_anic Guest

    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, Jan 8, 2007
    #6
  7. Prateek

    Tim Slattery Guest

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

    --
    Tim Slattery

    http://members.cox.net/slatteryt
     
    Tim Slattery, Jan 8, 2007
    #7
  8. Prateek

    Mark P Guest

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


    *Almost* any...

    http://www.parashift.com/c -faq-lite/operator-overloading.html#faq-13.5
     
    Mark P, Jan 8, 2007
    #8
  9. Prateek

    Prateek Jain Guest

    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!!!
     
    Prateek Jain, Jan 12, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,660
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,426
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    326
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    500
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    451
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page