C++Assignment OperatorsAddition Assignment

+= Assignment Operator in C++

In C++, the += operator is a compound assignment operator that adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand. This operator helps to write more concise and readable code.

This guide is aimed at complete beginners and will walk you through the usage of += with different data types, complete with step-by-step explanations.


Syntax

a += b; // Equivalent to: a = a + b;

How It Works

When you use a += b, it means:

  1. Take the current value of a.
  2. Add b to it.
  3. Store the result back into a.

Example with Integers

#include <iostream>
using namespace std;
 
int main() {
    int a = 10;
    int b = 5;
    a += b;
    cout << "a = " << a << endl;
    return 0;
}

Explanation:

  • a = 10, b = 5
  • a += b is the same as a = a + ba = 10 + 5
  • So a becomes 15

Example with Floating-Point Numbers (float/double)

#include <iostream>
using namespace std;
 
int main() {
    float x = 3.5f;
    float y = 1.2f;
    x += y;
    cout << "x = " << x << endl;
    return 0;
}

Explanation:

  • x = 3.5, y = 1.2
  • x += yx = x + yx = 3.5 + 1.2
  • x becomes 4.7

Example with Characters

#include <iostream>
using namespace std;
 
int main() {
    char ch = 'A'; // ASCII 65
    ch += 1;
    cout << "ch = " << ch << endl;
    return 0;
}

Explanation:

  • The character 'A' has ASCII value 65.
  • ch += 1ch = ch + 1 → ASCII value becomes 66
  • ASCII 66 is 'B', so ch becomes 'B'

Example with Strings (Using std::string)

#include <iostream>
#include <string>
using namespace std;
 
int main() {
    string greeting = "Hello, ";
    string name = "Alice";
    greeting += name;
    cout << "greeting = " << greeting << endl;
    return 0;
}

Explanation:

  • greeting = "Hello, ", name = "Alice"
  • greeting += namegreeting = greeting + name"Hello, " + "Alice"
  • So greeting becomes "Hello, Alice"

Example with Arrays (Not Directly Supported)

The += operator does not work directly with arrays in C++. Here’s an example showing incorrect usage:

int arr1[3] = {1, 2, 3};
int arr2[3] = {4, 5, 6};
// arr1 += arr2; //  Not allowed

Why?

In C++, arrays are not objects; they are raw blocks of memory. You must use loops to perform element-wise addition:

#include <iostream>
using namespace std;
 
int main() {
    int arr1[3] = {1, 2, 3};
    int arr2[3] = {4, 5, 6};
 
    for (int i = 0; i < 3; i++) {
        arr1[i] += arr2[i];
    }
 
    for (int i = 0; i < 3; i++) {
        cout << "arr1[" << i << "] = " << arr1[i] << endl;
    }
 
    return 0;
}

Example with Pointers

The += operator can be used with pointers to move the pointer forward by a number of elements.

#include <iostream>
using namespace std;
 
int main() {
    int arr[] = {10, 20, 30};
    int* ptr = arr;
    ptr += 1;
    cout << "*ptr = " << *ptr << endl;
    return 0;
}

Explanation:

  • ptr initially points to arr[0] (10)
  • ptr += 1 moves the pointer to arr[1] (20)
  • So *ptr prints 20

❗ Things to Remember

  1. The += operator modifies the left-hand variable.
  2. It can be used with most built-in types and with custom classes that overload the + and += operators.
  3. It improves code readability and reduces redundancy.

🧪 Summary Table

Data TypeSupported?Behavior Example
int✅ Yesa += b;a = a + b
float✅ Yesf += 1.5;
double✅ Yesd += 2.75;
char✅ Yes'A' += 1;'B'
std::string✅ Yes"Hello" += "World""HelloWorld"
Array❌ NoUse loops to handle element-wise ops
Pointer✅ Yesptr += 1; moves to next element

Custom Types

If you’re creating your own class, you can overload the += operator to define how it should behave:

class Counter {
    int count;
public:
    Counter(int c = 0) : count(c) {}
    Counter& operator+=(int val) {
        count += val;
        return *this;
    }
    void show() {
        cout << "count = " << count << endl;
    }
};
 
int main() {
    Counter c(5);
    c += 10;
    c.show(); // count = 15
    return 0;
}

Conclusion

The += operator is a powerful shorthand in C++ that improves code clarity. It behaves consistently across most primitive types and is also extendable to custom types. Understanding it thoroughly can make your code cleaner and easier to maintain.


Would you like this formatted into an .md file or converted to HTML for your documentation site?