C++ is a powerful programming language that provides a wide range of operators to perform various operations on data. Operators are symbols that are used to perform operations on variables and values. In this tutorial, we will discuss the different types of operators available in C++.
C++ operators can be classified into several categories based on their functionality. The following are the different types of operators available in C++:
Each of these operators has a specific purpose and syntax. Let's take a closer look at each of these operators.
Arithmetic operators are used to perform mathematical operations on variables and values. The following are the arithmetic operators available in C++:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus |
Here is an example of using arithmetic operators:
int a = 10;
int b = 20;
int c = a + b;
In this example, we are using the addition operator to add the values of variables a and b and store the result in variable c.
Relational operators are used to compare two values and return a boolean value (true or false). The following are the relational operators available in C++:
Operator | Description |
---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Here is an example of using relational operators:
int a = 10;
int b = 20;
bool result = (a > b);
In this example, we are using the greater than operator to compare the values of variables a and b and store the result in a boolean variable called result.
Logical operators are used to combine two or more conditions and return a boolean value (true or false). The following are the logical operators available in C++:
Operator | Description |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Here is an example of using logical operators:
int a = 10;
int b = 20;
bool result = (a > 5) && (b < 30);
In this example, we are using the logical AND operator to combine two conditions and store the result in a boolean variable called result.
Bitwise operators are used to perform operations on the binary representation of values. The following are the bitwise operators available in C++:
Operator | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
Here is an example of using bitwise operators:
int a = 10;
int b = 20;
int result = a & b;
In this example, we are using the bitwise AND operator to perform a bitwise AND operation on the values of variables a and b and store the result in variable result.
Assignment operators are used to assign values to variables. The following are the assignment operators available in C++:
Operator | Description |
---|---|
= | Simple assignment |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
&= | Bitwise AND and assign |
|= | Bitwise OR and assign |
^= | Bitwise XOR and assign |
<<= | Left shift and assign |
>>= | Right shift and assign |
Here is an example of using assignment operators:
int a = 10;
a += 5;
In this example, we are using the addition and assignment operator to add the value 5 to variable a.
Conditional operators are used to evaluate a condition and return one of two values based on the result of the evaluation. The following is the conditional operator available in C++:
Operator | Description |
---|---|
?: | Conditional operator |
Here is an example of using the conditional operator:
int a = 10;
int b = 20;
int result = (a > b) ? a : b;
In this example, we are using the conditional operator to evaluate the condition (a > b) and return the value of variable a if the condition is true, or the value of variable b if the condition is false.
Increment and decrement operators are used to increase or decrease the value of a variable by 1. The following are the increment and decrement operators available in C++:
Operator | Description |
---|---|
++ | Increment |
-- | Decrement |
Here is an example of using increment and decrement operators:
int a = 10;
a++;
In this example, we are using the increment operator to increase the value of variable a by 1.
Member access operators are used to access the members of a class or structure. The following are the member access operators available in C++:
Operator | Description |
---|---|
. | Member access operator |
-> | Pointer to member access operator |
Here is an example of using member access operators:
class MyClass {
public:
int myVar;
};
MyClass obj;
obj.myVar = 10;
In this example, we are using the member access operator to access the member variable myVar of the object obj.
Pointer operators are used to work with pointers. The following are the pointer operators available in C++:
Operator | Description |
---|---|
* | Dereference operator |
& | Address of operator |
Here is an example of using pointer operators:
int a = 10;
int* ptr = &a;
*ptr = 20;
In this example, we are using the address of operator to get the address of variable a and store it in a pointer variable called ptr. We are then using the dereference operator to access the value of the variable pointed to by ptr and assign it the value 20.
C++ provides a wide range of operators to perform various operations on data. Understanding these operators is essential for writing efficient and effective C++ code. We hope this tutorial has provided you with a good understanding of the different types of operators available in C++.