3

I don't quite understand what this means...I'm just learning C++ from my very very very basic Python experience...and so this may be a very stupid question. My question is...say you have your classic "Hello World" program and you have the line:

cout<<"Hello World!"<<endl;

what does the << mean...because I was just looking at using input in C and saw that you'd do something like:

int i;
cin>>i;

and I noticed that it has >> instead of << and I've read that those are bitwise shifts...and I don't exactly understand what those are...but I think it might be different here...Help...Thanks in advance

JacKeown
  • 2,780
  • 7
  • 26
  • 34
  • They're [operator overloads](http://stackoverflow.com/questions/4421706/operator-overloading). – Benjamin Lindley Feb 25 '11 at 23:08
  • What source of information are you using to learn C++? That source is where you encountered the instructions you're asking about, right Didn't it explain what they mean? – Rob Kennedy Feb 25 '11 at 23:48
  • Duplicate of [http://programmers.stackexchange.com/questions/52175/and-in-c](http://programmers.stackexchange.com/q/52175/6440) – Mateen Ulhaq Feb 26 '11 at 00:10
  • possible duplicate of [What is "operator<<" called?](http://stackoverflow.com/questions/3281666/what-is-operator-called) – Ben Voigt Feb 26 '11 at 05:50

5 Answers5

9

In Python, you can implement __lshift__ and __rshift__ to do whatever you want. In C++, it is the same - while the classic meaning is bitwise shift right and bitwise shift left, you can make it do whatever you want.

This is probably one of the most blatant violations of "sensible" operator overloading in C++, but that is just how std::ostream and std::istream work. For all of the C++ lovers out there (myself included), I apologize for this strange choice of operators. Just think of it as the direction that the data flows in (cout << foo puts a foo in cout, cin >> foo puts cin in foo), smile and be happy. From a newcomer, it really doesn't make sense, but drink the C++ Kool Aid and you'll be OH YEAH about it. Trust me.

Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
  • I suppose I count as Kool aid drinker, but I just wanted to point out there are valid technical reasons here compared to `printf`, not just style issues. A compiler can't tell if a variadic argument is NULL or 0, and format specifiers are subject to certain kinds of vulnerabilities and can't be easily extended to new types. The choice of operator overloading allows extensions to be associated with the class on the rhs, rather than having to extend `iostream`. A hack to be sure, but a prime example of an exception to the rule. – Karl Bielefeldt Feb 26 '11 at 00:16
  • 3
    You say that this choice of operators doesn't make sense to newcomers. I disagree. I've taught over 200 newcomers, and not one of them ever objected to it. If you want something that doesn't make sense to newcomers, it's *shifting bits*. – Rob Kennedy Feb 26 '11 at 00:50
  • 2
    @Rob, maybe he means newcomers to C++ who were oldcomers from another language where a bit shift operator is always a bit shift operator. Can't argue with you, though, about it confusing complete newbies. I saw a SO answer the other day where someone thought `>> 3` shifted 3 decimal digits if the lhs was in decimal. – Karl Bielefeldt Feb 26 '11 at 09:10
  • @Karl: he probably does, but this ought to be precised. – Matthieu M. Feb 26 '11 at 11:10
2

Operators can have different meanings when applied to different types, and C++ allows the developer to overload operators to do just that. It's a powerful technique that should be used cautiously, since it's really easy to make programs unreadable if you overload unjudiciously.

Therefore >> and << are bit shift operators when the left side is an integer, but input and output operators when the left side is an I/O stream or similar. Read them as arrows pointing which direction the data flows.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
2

Originally, it did mean bitshift. And you can still use it as a bitshift for an int (or other basic type).

Classes let you redefine operators. This allows you to create iterators, for which ++ actually does what you want (iterates to the next element), by modifying an internal member appropriately.

<< and >> are also operators, which can be redefined for classes, and this allows them to be used as "stream insertion/extraction operators".

The ostream class actually defines ostream& operator<< (Type); and the istream class defines istream& operator>> (Type&);

You can see more details about i/ostream here: http://www.cplusplus.com/reference/iostream/

Full details of ostream's operator<< and of istream's operator>>.

You can even write your own such operators to be able to do cout << myclass;:

class MyClass{ ... };

ostream& operator<< (ostream& os, const MyClass& myclass) {
  // code to insert it, usually something like:
  return os << myClass.a << ' ' << myClass.b;
}

// and now you can do this:
MyClass m;
std::cout << m;
Tim
  • 8,912
  • 3
  • 39
  • 57
0

The << and >> operators are bitshift operators, but, for input/output streams, they are overloaded (=>their meaning is redefined) so that they are used to insert/extract stuff into the streams.

I think they were used because they look intuitive (they indicate the direction of the stream of data) and because they are almost never used for other purposes.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • It depends on how you see it. In the C++ standard library there are hundreds of overloads where << and >> are the I/O operators, so this is conceivably the standard use. That they also can be used for some odd binary bit shifting on a few built in types, is the exception. :-) – Bo Persson Feb 26 '11 at 10:02
  • @Bo: I refer to the function they were born for (in C), in facts I said that "they are almost never used for other purposes". On the other hand, try to ask to anybody developing in C++ on microcontrollers if `<<` is bit shift or insertion operator. `:)` – Matteo Italia Feb 26 '11 at 15:29
0

Instead of saying

print "Hello" + "world"  #I know, you wouldn't actually do it like this.

where "print" is the command and "hello world" is your text,

c++ works as

cout << "Hello" << "World";  // or like this. But...

int i = 42;
cout << "The answer is: " << i;  //you may want to do this

c++ uses "cout" as the command and "<<" as a joiner, or, more technically, an operator used on the out stream.

cin works similarly where the >> is just an operator that works on the in stream.

emragins
  • 4,607
  • 2
  • 33
  • 48