C++ Numbers and Strings

In C++, the + operator serves two distinct purposes: adding numeric values and combining (concatenating) text values (strings). This is a powerful feature, but knowing how C++ treats numbers and strings differently is vital to avoid errors.

Tutorials dojo strip

Adding Numbers

When the + operator is used with numbers, C++ performs mathematical addition.

Example

C++

Output: This program calculates and displays “Total RPM: 10900”.

Combining Strings

When the + operator is used with strings, it performs concatenation, joining strings to form one.

Example

C++

Output: This combines “Shoei “ and “Full Face” to display “Helmet Info: Shoei Full Face”.

Adding Numbers and Strings Together

Attempting to combine numbers and strings directly with the + operator will cause an error, as C++ cannot perform both addition and concatenation simultaneously.

Example

C++
Tutorials dojo strip