< Previous | Contents | Next >
Next, I create a new string object, phrase, by concatenating the first three
string objects:
string phrase = word1 + " " + word2 + word3;
As a result, phrase is "Game Over!!!".
Notice that the + operator, which you’ve seen work only with numbers, also concatenates string objects. That’s because the + operator has been overloaded. Now, when you first hear the term overloaded, you might think it’s a bad thing—the operator is about to blow! But it’s a good thing. Operator over- loading redefines a familiar operator so it works differently when used in a new, previously undefined context. In this case, I use the + operator not to add numbers, but to join string objects. I’m able to do this only because the string type specifically overloads the + operator and defines it so the operator means string object concatenation when used with strings.