Article Contents
1. Operators in C++
2. Types of Operators in C++
3. Assignment Operator in C++ (=)
4. Arithmetic Operators in C++
5. Relational Operators in C++
6. Logical Operators in C++
7. Bitwise Operators in C++
8. Shift Operators in C++
9. Unary Operators in C++
10. Ternary Operators in C++
11. Comma Operator in C++
Operators in C++
Operators are special functions with one or more parameters that generate a new value. For example, plus (+), minus (-), multiply (*), ... are all operators. Operators are used to perform various operations on different variables and constants.
Types of Operators in C++
- Assignment Operator
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Shift Operators
- Unary Operator
- Ternary Operator
- Comma Operator
Assignment Operator in C++ (=)
The '=' operator is used for assignment, it takes the right-hand side (known as rvalue) and copies it to the left-hand side (known as lvalue). The assignment operator is the only overloadable operator but cannot be inherited.
Arithmetic Operators in C++
Operators used to perform basic arithmetic calculations. Addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) are basic operators. The Modulus operator cannot be applied to floating-point numbers.
Additionally, C++ and C also utilize shorthand notations to perform operations and assignments of the same type.
For example:
int x=10;
x + = 4 // will add 4 to 10 and assign 14 to X.
x - = 5 // will subtract 5 from 10 and assign 5 to x.
Relational Operators in C++
Relational operators in C++ establish relationships between operands. Relational operators in C++ include: less than (<), greater than (>), less than or equal to (<=), greater than or equal to (>=), equal to (==), and not equal to (!=).
A small note to remember is that the assignment operator (=) and the equality operator (==) in relational operators are different. The assignment operator is used to assign a value to any variable, whereas the equality operator is used to compare values, similar to if-else conditions.
Below is an example of relational operators in C++:
int x = 10; //assignment operator
x=5; // assignment operator
if(x == 5) // here we use the equality operator to compare
{
cout <'comparison=' becomes=''>
}
Logical Operators in C++
Logical operators include AND (&&) and OR (||) used to combine two different expressions together.
When using the AND operator to connect two statements, the validity of both statements will be considered, but if combined with the OR operator, either of the statements must be valid.
These operators are mainly used in loops (especially while loops) and in conditional statements.
Bitwise Operators in C++
Bitwise Operators in C++ are used to perform operations on individual bits. They only work with discrete data types like char, int, and long, not with floating-point values.
- Bitwise AND operator (&).
- Bitwise OR operator (|).
- Bitwise XOR operator (^).
- Bitwise NOT operator (-).
Additionally, they can be used as shorthand notations, &=, |=, ^=, -=, ... .
Shift Operators in C++
Shift operators are used to alter the bits of any variable, including 3 types:
- Left shift operator <>
- Right shift operator >>.
- Unsigned right shift operator >>>.
Unary Operators in C++
Unary operators in C++ are operators that only operate on one operand. There are several different unary operators, among which ++ and -- are the most commonly used.
Additionally, there are several other unary operators such as $, *, new and delete, bitwise not operator -, logical not operator !, subtraction unary operator (-), and addition unary operator (+).
Ternary Operators in C++
The if-else ? : operator is a ternary operator with 3 operands.
Below is an example of ternary operator in C++:
int a = 10;
a > 5 ? cout < 'true'='' :='' cout=''><>
Comma Operator in C++
This operator is used to separate variable names and expressions. In the case of expressions, the value of the final expression is generated and utilized.
For example of Comma operator in C++:
int a,b,c; // declaring variables using comma operator
a=b++, c++; // a = c++ will be executed.
So, in the above article, Mytour just introduced you to the operators in C++. In the upcoming articles, Mytour will further introduce you to what loops are in C++.
Additionally, readers can refer to some other articles already available on Mytour to understand more about storage class in C++.
