C++ Operators
C++ Operators
Operators are special type of functions, that takes one or more arguments and produces a new value. Foe example: addition (+), Subtraction (-), multiplication(*) etc, are all operators. Operators are used to perform various operations on variables and constants.
Types of operators :
- Assignment Operator
- Mathematical Operators
- Relational Operators
- Logical Operators
- Bit-wise Operators
- Shift Operators
- Unary Operators
- Ternary Operator
- Comma Operator
Assignment Operator (=)
Operators'=' is used for assignment, it takes the right-hand side (called rvalue) and copy it into the lefthand side (called value). Assignment Operator is the only operator which can be overlosded but cannot be inherited.
Mathematical Operators
There are operators used to perform basic mathematical operations. Addition (+),Subtraction (-), multiplication(*) and modulus(%) are the basic mathematical operators. Modulus operator cannot be used with floating-point numbers.
C++ and C also use a short-hand notation to platform an operation and assignment at same type. Example,
int x=19;
x += 4 //will add 4 to 10, and hence assign 14 to x,
x -= 5 // will subtract 5 from 10 and assign 5 to x,
Relational Operators
These operators establish a relationship between operands. The relational operators are :
less than(<), greater than (>), less than or equal to(<=), greater than or equal to (>=), equivalent (==) and not equivalent(!=).
You must notice that assignment operator is (=) and thre is a relational operator, for equivalent(==). These two are different from each other, the assignment operator assigns the value to any variable, whereas equivalent operaator is used to compare values, like in if-else conditions , Example:
int x=10; //assigmnent operator
x=5; // again assignment operator
if (x==5) // here we have used equivalent relational operator, for comparison
{
count <<"Successfully compared";
}
Logical Operators
The logical operators are AND (&&) and OR (||). They are used to combine two different expressions together.
If two statement are connected using AND operator, the validity of both statements will be considered, but if they are connected using OR operator, then either one of them must be valid. These operators are mostly used in loops (espicially while loop) and in Decision making.
Bitwise Operators
They are used to change individual bits into a number. They work with only integral data types like char,init and long and not with floating point values.
1. Bitwise AND operators &
2. Bitwise OR operators |
2. Bitwise XOR operator ^
4. Bitwise NOT operator ~
They can be used as shorthand notation too, &= , |=, ^=, ~= etc.
Shift Operators
Shift Operators are used to shift Bits of any variable. it is of three typs,
1. Left Shift Operators <<
2. Right Shift Operators >>
3. Unsigned Right Shift Operators >>>>
Unary Operators
These are the operators which work on only one operand. There are many unary operators, but increment ++ and decrement -- operators are most used.
Other Unary Operators : address of &, dereference *, new and delete, bitwise not ~, logical not !, unary minus - and unary plus + .
Ternary Operator
The ternary if - else ? : is an operator which has three operands.
int a = 10;
a > 5 cout <<"true" ! cout << "false"
Comma Operator :
This is used to separate variable names and to separate expressions. In case of expresssions, the value of last expression is produced and used.
Example :
int a,b,c; // variables declaration using comma operator
a=b++, c++; // a= c++ will be done.
0 comments:
Post a Comment