< Previous | Contents | Next >

Concatenating string Objects

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 youve seen work only with numbers, also concatenates string objects. Thats because the + operator has been overloaded. Now, when you first hear the term overloaded, you might think its a bad thingthe operator is about to blow! But its 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. Im 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.