![]() |
| | #1 |
| C++ n00b Join Date: Aug 2004
Posts: 5
| 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++ |
| eviscerator is offline | |
| | #2 |
| Sweet Join Date: Aug 2002 Location: Tucson, Arizona
Posts: 1,678
| 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)
__________________ Woop? |
| prog-bman is offline | |
| | #3 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| 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; Code: Foo object;
ofstream ofile("test.txt");
ofile << object;
Well, can you think of a better operator? |
| The Dog is offline | |
| | #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) |
| bartybasher is offline | |
| | #5 |
| Hardware Engineer Join Date: Sep 2001
Posts: 1,397
| 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
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. |
| DougDbug is offline | |
| | #6 |
| Banned Join Date: May 2004
Posts: 55
| Code: cin >> x // Put in information in X cout << x // Put out information in X |
| Noxir is offline | |
| | #7 | |
| Registered User Join Date: Jul 2003
Posts: 1,088
| Quote:
| |
| jlou is offline | |
| | #8 | |
| Useless Apprentice Join Date: Jun 2004
Posts: 76
| Quote:
| |
| ryan_germain is offline | |
| | #9 |
| VA National Guard Join Date: May 2004 Location: Manassas, VA USA
Posts: 902
| our book mentions these operators as the >> extraction and << insertion operators. fyi.
__________________
|
| The Brain is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Friend func. and overloaded >> and << | Kheila | C++ Programming | 5 | 12-02-2005 01:14 AM |
| << !! Posting Code? Read this First !! >> | kermi3 | Linux Programming | 0 | 10-14-2002 01:30 PM |
| << !! Posting Code? Read this First !! >> | kermi3 | Windows Programming | 0 | 10-14-2002 01:29 PM |
| << !! Posting Code? Read this First !! >> | kermi3 | Game Programming | 0 | 10-14-2002 01:27 PM |
| << !! Posting Code? Read this First !! >> | kermi3 | C# Programming | 0 | 10-14-2002 01:26 PM |