C++ Addition

In C++, Addition is performed using arithmetic operator +. The operator takes two operands and returns the sum of two operands.

In this tutorial, we shall learn how to use Arithmetic Addition Operator with values of different datatypes using example programs.

C++ Addition of Two Integers

You can add two integers using addition operator. The datatype of the operands and returned value is given in the following code snippet.

int = int + int

In the following program, we initialize two integer variables and add them using addition operator.

C++ Program

#include <iostream>  
using namespace std;

int main() {
   int a = 12;
   int b = 7;

   int sum = a + b;

   cout << sum << endl;
}

Output

19

C++ Addition of Two Floating Point Numbers

You can add two floating point numbers using addition operator. The datatype of the operands and returned value is given in the following code snippet.

float = float + float

In the following program, we initialize two floating point variables and add them using addition operator.

C++ Program

#include <iostream>  
using namespace std;

int main() {
   float a = 1.2;
   float b = 0.6;

   float sum = a + b;

   cout << sum << endl;
}

Output

1.8

C++ Addition of Integer and Floating Point Number

You can add an integer and floating point number using addition operator. The datatype of the operands and returned value is given in the following code snippet.

float = int + float

In the following program, we initialize an integer variable and a floating point variable and add them using addition operator.

C++ Program

#include <iostream>  
using namespace std;

int main() {
   int a = 1;
   float b = 2.6;

   float sum = a + b;

   cout << sum << endl;
}

Output

3.6

C++ Addition of two Chars

You can add two characters using addition operator. The datatype of the operands and returned value is given in the following code snippet.

char = char + char

In the following program, we initialize two character variables and add them using addition operator.

C++ Program

#include <iostream>  
using namespace std;

int main() {
   char ch1 = 12;
   char ch2 = 56;

   char ch = ch1 + ch2;

   cout << ch << endl;
}

Output

D

C++ Addition of two Booleans

You can add two boolean values using addition operator. The operator converts false to 0, true to 1 and then performs addition operation. The datatype of the operands and returned value is given in the following code snippet.

bool = bool + bool

Arithmetic Addition on two boolean values is equivalent to logical OR operation.

In the following program, we initialize two boolean variables and add them using addition operator.

C++ Program

#include <iostream>  
using namespace std;

int main() {
   bool a = true;
   bool b = false;

   bool c = a + b;

   cout << c << endl;
}

Output

1

Chaining of Addition Operator

You can chain Addition Operator and perform the addition of more than two operands in a single statement. The sudo code is given below.

result = operand_1 + operand_2 + operand_3 + ... + operand_n

In the following example, we shall take four integer variables and add them altogether in a single statement using arithmetic addition operator.

C++ Program

#include <iostream>  
using namespace std;

int main() {
   int a = 12;
   int b = 25;
   int c = 61;
   int d = 37;

   int sum = a + b + c + d;

   cout << sum << endl;
}

Output

135

Conclusion

In this C++ Tutorial, we learned how to use C++ Addition Operator on values of different datatypes, etc.