Operators in C++
An expression consists of variables and constants separated by Operators in C++. There are mainly 8 types of Operators in C++ are given as follows :
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Increment and decrement operators
5. Pointer Operators
6. Assignment Operators
7. Bitwise Operators
8. Misc Operators
Arithmetic Operators : Arithmetic Operators are used to perform arithmetic operations.
Relational Operators : Relational Operators are used to compare the values of operands to produce a logical value. It is a type of bool so, a logical is either true or false.
Logical Operators : Logical Operators combine two or more relational operators or expressions and produce a logical value or results.
x>y && x==5 x==5 || y==7
Increment and decrement operators : Increment operator (++) is used to increase the value of an integer or char variable by 1. Decrement operator (- -) is used to reduce the value of an integer or char variable by 1
x = 15; x++ or ++x will produce the result x = 16 x = 15; x- – or – -x will produce the result x = 14
Pointer Operators : Pointer Operators produces address values.
&a ptr ptr+1 "ABC"
where a is a variable and ptr is a pointer.
Assignment Operators : In C++, there are three types of Assignment Operators are given as below :
(i) Chained Operators : A chained operator is used for instance variable at the time of declaration. It can’t used to initialize varables.
a=(b=5); or a=b=5;
(ii) Embedded Operators :
a=(b=10)+5; (b=10) is an assignement operator that is known as Embedded Operator, Here the value 10 is assigned to b and then the result 10+5=15 is assigned to a.
(iii) Compound Operators : In C++, compound operator is a combination of the assignment operator with a binary arithmetic operator.
a+=5; or a=a+5; The operator += is known as Compound Assignment Operator.
Bitwise Operators : Bitwise Operators are used to manipulate data at bit level. They are basically used for testing or shifting bits.
a<<4 // shift four bit position to left b>> 2 // shift two bit position to right