Thread: The >> and << thingies..

  1. #1

    Question The >> and << thingies..

    Hi, i'm new to this forum. I've been developing applications (mostly games) since the age of 12, but mostly with Click software (www.clickteam.com) which is much easier than programming it (though that's beside the point of this post).

    My intent on posting this is that i've been getting into C++ recently, and i'm wondering about the >> and << parts of "cout" and "cin" lines...

    for example, if i write this in my code:
    cout << "Moo"; - then it has <<

    if i need input from the user it's like
    cin>>SomeVariable; - and there's the >>

    So my question is: Why are they there and what do they mean ? to me they seem pointless compared to Php which is a lot like C++

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    They are operators. They are not pointless they make life much easier you'll understand more when you get into c++ a little further.
    You could just as well do this
    Code:
     cout.write("Hi",2)
    but then you would need to specify how much space you are using each time which would not be fun now would it.
    Woop?

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    It's there to make your life easier. Understand the power of that overloaded operator.

    The << and >> are bitwise operators known as the left-shift and right-shift operators.

    By overloading these operators, you could have constructs such as the following:
    Code:
    Foo object;
    int ival;
    double dval;
     
    //..
    //..
     
    cout << object << " " << ival << " " << dval;
    You could write your own classes that overload the << or >> operators , then you could use it with cout, or cin, or even with a file, eg.
    Code:
    Foo object;
    ofstream ofile("test.txt");
    ofile << object;
    But why were the << and >> used for cin, cout etc?
    Well, can you think of a better operator?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Hi I have used the clickteam tools a lot of my life too. So I probably know where you are coming from with a lot of things regarding C++. So if you ever have any more problems dont hesittate to email me and I will try and explain things in terms of those programs.
    (Steiscool from the clickteam forum)

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Just accept the syntax!

    Every language has it's own syntax, and cin & cout require these operators. This is the simply the way the language is structured and defined by the creators of C/C++. In the C language, input/output has to be done with a function. In C++, you have the option of using overloaded (re-defined) operators.

    When used with cin & cout (iostream), these two operators are the insertion (>>) and extraction (<<) operators.

    Code:
    cout << X ;         // This is C++
    cout X ;            // This is an ERROR!
    printf("%d", X) ;   // This is C (Also valid in C++)
    PRINT X              ' This is BASIC
    Don't worry about the fact that these are overloaded operators. (You do need to know that when used with cin and cout, they are NOT the shift-left and shift-right operators.)

    About overloading operators -
    In C++ you can re-define an operator. (You can even make the + operator subtract!) Normally, you use overloading for something special... For example, you could use the + operator to "add" two images together in a way defined by your program.
    Last edited by DougDbug; 08-05-2004 at 12:50 PM.

  6. #6
    Banned
    Join Date
    May 2004
    Posts
    55
    Code:
     cin >> x          // Put in information in X
    cout << x                   // Put out information in X
    easy? xD

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by DougDbug
    When used with cin & cout (iostream), these two operators are the insertion (>>) and extraction (<<) operators.
    There was just a little discussion about this in another thread, and I think you have them switched - inserter (<<) and extractor (>>).

  8. #8
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by jlou
    There was just a little discussion about this in another thread, and I think you have them switched - inserter (<<) and extractor (>>).
    doug, jlou is right...i learned the hard way. lol

  9. #9
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    our book mentions these operators as the >> extraction and << insertion operators. fyi.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend func. and overloaded >> and <<
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2005, 01:14 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM