In C, the plus sign ( ) is used to perform addition operations and can be applied to numbers, strings, and custom data types: Numeric addition: Add two or more numbers. String concatenation: Concatenate two or more strings together. Addition of custom data types: After overloading the plus sign, objects of custom data types can be added.
The plus sign ( ) in C
The plus sign ( ) in C is an operator used to perform addition operations. It can be applied to numbers, strings, and custom data types.
Applied to numbers
When applied to numbers, the plus sign adds two or more numbers and returns the result. For example:
<code class="c++">int num1 = 10; int num2 = 5; int sum = num1 + num2; // sum = 15</code>
applied to string
When applied to a string, the plus sign concatenates two or more strings together and returns the concatenated string. For example:
<code class="c++">string str1 = "Hello"; string str2 = "World"; string greeting = str1 + str2; // greeting = "HelloWorld"</code>
applies to custom data types
The plus sign can also be overloaded as a custom data type. When overloaded, it allows objects of custom data types to be added in a manner similar to numbers or strings. For example, suppose we have a custom type called Point
that represents a 2D point:
<code class="c++">class Point { public: int x; int y; Point operator+(const Point& other) { return {x + other.x, y + other.y}; } };</code>
Now we can add Point
objects like this:
<code class="c++">Point point1 {1, 2}; Point point2 {3, 4}; Point sum = point1 + point2; // sum = {4, 6}</code>
Other Applications
In addition to arithmetic and string concatenation, the plus sign can be used for other purposes:
The above is the detailed content of What is the symbol for sum in c++. For more information, please follow other related articles on the PHP Chinese website!