+ 6
Characters can be added, but the result will not be a string. Eg - cout<<'0'+'A'; // Prints char(113) or 'q', not "0A". This is because the operator treats them as integers and simply adds their ASCII values instead. So '0'(48) + 'A'(65) = 'q'(113). To add a character to form a string, you will have to declare a string with a single character, and then you can append the character using +. Eg - cout<<string("a")+'b'; // Prints ab, not char(195). // Notice the double quotes and the // cast to C++03 string. And to add a character to a C-string (a char*), you need to convert the second character to a string, and use strcat(). Lastly, you may declare a new class and overload the + operator for the same.
25th Dec 2017, 3:10 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar