4

For example:

int age;
cin >> age;

cout << "You are " << age << " years old!" << endl;

Why do we use the "<<" and ">>" operators here? What are they doing? I somewhat understand bit-shifting, but I don't get how that works here.

CodeMan42
  • 77
  • 5
  • 1
    Read any book on C++. It will explain everything to you in the first chapter, or two. – Sam Varshavchik May 25 '16 at 01:13
  • [Great list of C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Captain Obvlious May 25 '16 at 01:14
  • 3
    It's not helpful to tell someone to "just go read the book" when they have a particular question about just one aspect of a language. The OP clearly knows at least _some_ C++. You can't search in Google or the index of a book for something when you don't know what it's called (operator overloading in this case). – Ates Goral May 25 '16 at 01:21
  • 2
    @SamVarshavchik Operator overloading _will most likely not_ be explained in the first chapter or two. Try > 10 or 20. – Ates Goral May 25 '16 at 01:23
  • 1
    The first thing I expect to see in any C++ book that's worth the trees that were sacrificed to make it is: `std::cout << "Hello world" << std::endl;` followed by an explanation. – Sam Varshavchik May 25 '16 at 01:25

3 Answers3

11

They are called the stream insertion operator (<<) and the stream extraction operator (>>).

These are the same operators as the left and right bit shift operators (even though they have different names). The bit shift operators are overloaded, so that when the left side is a stream, they read from or write to that stream.

They're just like any function call - it works like:

leftShift(leftShift(leftShift(leftShift(cout, "You are "), age), " years old!"), endl);

except that the function is called operator<< instead of leftShift.
Strictly speaking, there's no reason that a function called leftShift has to do a left shift, and likewise there's no reason a function called operator<< has to do a left shift.

user253751
  • 57,427
  • 7
  • 48
  • 90
4

>> and << have been overloaded as stream functions such as:

std::ostream & operator << (std::ostream &, int) 

(and others)

so that (in this case) when cout << 10 is used, it calls the overloaded function that will print the value.

It has nothing to do with bit shifting except that it uses the same operator '<<' and '>>'.

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
lostbard
  • 5,065
  • 1
  • 15
  • 17
3

It's operator overloading. The bitshift operators are overloaded for the stream classes to serve a different purpose (of reading from and writing to streams). See: http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • And what is that purpose in those cases? Is it anything past "cout will output these things" and "cin will make that integer equal to the input" or is there a more specific thing going on? – CodeMan42 May 25 '16 at 01:17
  • 1
    It's pretty much just syntactical sugar. The overloaded operators are also chainable (i.e. they return the stream object instance) and therefore you can make a series of `<<` calls in a chain. It just looks concise. – Ates Goral May 25 '16 at 01:18