C++ Operators are special symbols that are used to operate different mathematical and logical operations. These operations are performed on variables and constants. These variables and constants are called Operands. Operators help to solve mathematical equations. There are different types of operators used in the C++ programming language. Following are their types:
- Assignment Operators
- Extended-Assignment Operator
- Arithmetic Operator
- Relational Operators
- Logical Operators
- Bitwise Operators
- Compound assignment operators
- Member and pointer operators
- Misc Operators
Assignment Operators
The assignment C++ operators assign value to the variables or constants. Its symbol is “=”. The assignment of values to variables is done from right to left only. The operand is present on the left side of the operator. While its value is present on the right. Multiple assignments can be done in the same line. The syntax for the assignment of the value to variables using assignment operators is as follows:
This piece of code will help to get a better understanding of how the assignment operator works:
#include <iostream> using namespace std; int main () { int a, b; // a and b are two integer variables a = 10; // value 10 is being assigned to variable a b = 4; // value 4 is being assigned to variable b a = b; // a is being overwritten and value of b is assigned to a b = 7; // value 7 is being assigned to variable b cout << "a:"; cout << a; cout << '/n'; cout << " b:"; cout << b; }
The output of the above code is:
Extended Assignment operator
These assignment C++ operators assign a value to a variable by applying pre-operations. There are the following assignment operators in C++, where A and B are two operands.
Operator |
Description |
Example |
+= | Add and assignment operator
This operator adds the right side operand to the left side operand and assigns the result to the left side operand. | B += A is equivalent to B = B + A |
-= | Subtract and assignment operator
This operator subtracts the right side operand from the left side operand and assigns the result to the left operand. | B -= A is equivalent to B = B – A |
*= | Multiply and assignment operator
This operator multiplies the right side operand with the left side operand and assigns the result to the left side operand. | B *= A is equivalent to B= B * A |
/= | Divide and assignment operator
This operator divides the left side operand with the right side operand and assigns the result to the left side operand. | B /= A is equivalent to B = B / A |
%= | Modulus and assignment operator
This operator takes modulus using two operands and assigns the result to the left operand. | B %= A is equivalent to B = B % A |
<<= | Left shift and assignment operator
This operator shifts the left side operand value by a number on the right side and assigns the result to the left side operand. | B <<= 2 is same as B = B << 2 |
>>= | Right shift and assignment operator.
This operator shifts the left side operand value by a number on the right side and assigns the result to the left side operand. | B >>= 2 is same as B = B >> 2 |
&= | Bitwise AND and assignment operator.
This operator performs the bitwise AND operation. | B &= 2 is same as B = B & 2 |
^= | Bitwise exclusive OR and assignment operator
This operator performs the bitwise X-OR operation. | B ^= 2 is same as B = B ^ 2 |
|= | Bitwise inclusive OR and assignment operator.
This operator performs the bitwise inclusive-OR operation. | B |= 2 is same as B = B | 2 |
They are also called Compound Assignment Operators.
Arithmetic Operators
Arithmetic Operators are mathematical C++ operators that evaluate mathematical expressions. C++ supports the following arithmetic operators.
Operator | Description |
+ | Adds two operands |
– | Subtracts the second operand from the first |
* | Multiplies both operands |
/ | Divides numerator by denominator |
% | Modulus Operator gives the remainder of integer division |
++ | Increment operator increases integer value by one |
— | Decrement operator decreases integer value by one |
For example, if two variables A and B hold values 10 and 5 respectively then the following operations produce the following results.
#include <iostream> using namespace std; int main () { int a, b, c, d, e, f, g; a = 10; b = 5; c = a + b; d = a - b; e = a * b; f = a / b; g = a % b; cout << "The sum of a and b is " << c << endl; cout << "The difference of a and b is " << d << endl; cout << "The product of a and b is " << e << endl; cout << "The result of a divided by b is " << f << endl; cout << "The result of a modulus b is " << g << endl; return 0; }
The output of the above code is:
The sum of a and b is 15 The difference of a and b is 5 The product of a and b is 50 The result of a divided by b is 2 The result of a modulus b is 0
You can edit this code and check further manipulations.
Relational Operators
Relational C++ Operators perform comparisons between two operands. They define the relation between variables. For instance, two operands can be equal or unequal to each other. One operand can be smaller or larger than the other. Relational operators like ==, <=, >= represent these relations. The result of relational operators can be either true or false. It depends on the value of the operands. The table below shows the relational operators and their definitions along with examples. Consider variable A has value 5 and variable B has value 8.
Operator |
Description |
Example |
== | Compares the value of two operands checks if they are equal. | A is not equal to B here, hence the relation A==B becomes false. |
<= | Compares if the value of the left side operator is smaller than or equal to the value of the operator on right. | A is smaller than B, hence the relation A<=B becomes true. |
>= | Compares if the value of the left side operator is greater than or equal to the value of the operator on right. | A is neither greater nor equal to B, hence the relation A>=B becomes false. |
!= | Compares the value of two operands and checks if they are not equal. | A is not equal to B here, hence the relation A!=B becomes true. |
< | Compares if the value of the left side operator is smaller than the value of the operator on right. | A is smaller than B, hence the relation A<B becomes true. |
> | Compares if the value of the left side operator is greater than the value of the operator on right. | A is smaller than B, hence the relation A>B becomes false. |
These operators are also called Comparison Operators.
Logical Operators
Logical C++ Operators are the symbols that perform logical operations like AND, OR on variables. The result of logical operations can either be true or false. True value returns 1 while False value returns 0. There are 3 types of logical operators:
- AND
- OR
- NOT
AND Operator (&&)
The logical operator && represents Boolean logical operator AND. It holds true value only when both A and B are true. Otherwise, it’s false. Following are the possible cases of A&&B operations:
A | B |
A&&B |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
OR Operator (||)
The logical operator || represents Boolean logical operator OR. It holds true value when there is at least one true value. It’s false when both A and B are false. Otherwise, it’s true. Following are the possible cases of A||B operations:
A | B |
A||B |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
NOT Operator (!)
The logical ! operator represents the Boolean logical operator NOT. Its function is to reverse the original result. NOT changes the true result to false and vice versa. It is written at the start of an expression. Following are the possible cases of !A operations:
A |
!A |
true | false |
false | true |
The below code will help get a deeper understanding of logical operations:
#include <iostream> using namespace std; int main() { int a = 2; int b = 5; cout << ((a<b) && (a==2)) << endl; // 1 is for True cout << ((a>b) || (b==5)) << endl; // 1 is for True cout << !((a>b) || (a==2)) << endl; // 0 is for False cout << ((a>b) && (a==5)) << endl; // 0 is for False return 0; }
The output of the above code after compilation is:
1 1 0 0
Bitwise Operators
Bitwise C++ operators perform bitwise operations on binary numbers. The logical bitwise operators have symbols &, |, ^, ~ for AND, OR, XOR, NOT respectively. The table below shows the symbols of bitwise operators along with their definitions.
Operator | Assembly language Operation |
Description |
& | AND | Performs bitwise AND operation. |
| | OR | Performs bitwise inclusive OR operation. |
^ | XOR | Performs bitwise exclusive OR (XOR) operation. |
~ | NOT | Unary complement operator performs bit inversion. |
<< | SHL | Bitwise shift left operator shifts bit to the left. |
>> | SHR | Bitwise shift right operator shifts bit to the right. |
A truth table will further explain the working of bitwise operators:
A | B | A&B | A|B | A^B |
~A |
0 | 0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
0 |
1 | 1 | 1 | 1 | 0 |
0 |
Consider two variables A and B having values 10 and 15 respectively. Their binary values will be:
A = 1010
B = 1111
The result of bitwise operations on A and B will be:
A&B = 1010 equals 10
A|B = 1111 equals 15
A^B = 101 equals 5
~A = -1011 equals -11
Miscellaneous Operators
Miscellaneous or Misc operators are predefined functions in C++. Following is the list of Misc C++ Operators used.
Operators |
Description |
sizeof | sizeof operator determines the size of variable or array. |
Condition ? A : B | Condition ? A : B determines the truthness of the condition stated and then returns variable A if the condition is true. If the condition is false, it returns B. |
, (Comma Operator) | Comma operator (,) separates two entities from one another. It determines the value of the last operand and returns its result. It discards the values of former operands. |
* | Pointer operator (*) works as a pointer that points to a certain variable. |
. | Dot operator (.) provides a reference to class, structure, or union. |
-> | Arrow operator (->) provides a reference to class, structure, or union. |
Cast | Cast Operator converts one data type to another. |
& | & Operator returns the address of a variable. |
Pointer-to-Member Operators
The pointer-to-member operators bind a pointer to a specific class object. The C++ pointer-to-members operators are .* and ->*. The syntax of writing these operators is.
expression .* expression expression ->* expression
The left side of the operator refers to the object of the specific class. While the right side refers to the member of the class. As their name suggests, pointer-to-member operators have a behavior similar to pointers. But pointer-to-members are used to achieve overloading. As they point to nonstatic members of a class. And can return objects having different data types and arguments than the nonmember functions of the same class.
Members of the class can be either variables or functions. The below code is an example of the use of pointer-to-member C++ operators.
#include <iostream> using namespace std; //define class class TArt { public: void display_func() { cout << "display_func\n"; } int var; }; //define pointer for function void (TArt::*ptr1)() = &TArt::display_func; //define pointer for data int TArt::*ptr2 = &TArt::var; int main() { TArt TArt1; TArt *ptrTArt = new TArt; //calling function through pointer-to-member operator (TArt1.*ptr1)(); //calling function through pointer-to-member operator (ptrTArt->*ptr1)(); TArt1.*ptr2 = 5; ptrTArt->*ptr2 = 10; cout << TArt1.*ptr2 << endl << ptrTArt->*ptr2 << endl; delete ptrTArt; }
After compilation, the output of the above code is.
display_func display_func 5 10
Logical Operators
Bitwise Operators
Compound assignment operators
Member and pointer operators
Misc Operators