C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-05-2004, 04:24 AM   #1
C++ n00b
 
eviscerator's Avatar
 
Join Date: Aug 2004
Posts: 5
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++
eviscerator is offline   Reply With Quote
Old 08-05-2004, 04:53 AM   #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)
but then you would need to specify how much space you are using each time which would not be fun now would it.
__________________
Woop?
prog-bman is offline   Reply With Quote
Old 08-05-2004, 05:10 AM   #3
Registered User
 
The Dog's Avatar
 
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;
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?
The Dog is offline   Reply With Quote
Old 08-05-2004, 09:48 AM   #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   Reply With Quote
Old 08-05-2004, 12:41 PM   #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
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.
DougDbug is offline   Reply With Quote
Old 08-05-2004, 06:53 PM   #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
Noxir is offline   Reply With Quote
Old 08-05-2004, 07:09 PM   #7
Registered User
 
jlou's Avatar
 
Join Date: Jul 2003
Posts: 1,088
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 (>>).
jlou is offline   Reply With Quote
Old 08-06-2004, 06:52 AM   #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
ryan_germain is offline   Reply With Quote
Old 08-06-2004, 10:50 AM   #9
VA National Guard
 
The Brain's Avatar
 
Join Date: May 2004
Location: Manassas, VA USA
Posts: 902
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
The Brain is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22