C++ Getalle en Strings


Voeg getalle en stringe by

WAARSKUWING!

C++ gebruik die +operateur vir beide optelling en aaneenskakeling .

Getalle word bygevoeg. Strings is aaneengeskakel.

As jy twee getalle bytel, sal die resultaat 'n getal wees:

Voorbeeld

int x = 10;
int y = 20;
int z = x + y;      // z will be 30 (an integer)

As jy twee stringe byvoeg, sal die resultaat 'n string aaneenskakeling wees:

Voorbeeld

string x = "10";
string y = "20";
string z = x + y;   // z will be 1020 (a string)

As jy probeer om 'n nommer by 'n string te voeg, vind 'n fout plaas:

Voorbeeld

string x = "10";
int y = 20;
string z = x + y;