An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Increment & decrement operator
- Assignment Operators
- Conditional operator.
- The Comma (,) operator.
- Sizeof operator.
- Scope resolution operator (::)
- ‘new’ operator
1) Arithmetic Operators: Arithmetical operators +, -, *, /, and % are used to
performs an arithmetic (numeric) operation. You can use the operators +,
-, *, and / with both integral and floating-point data types.
Modulus or remainder % operator is used only with the integral data
type.
2) Relational operators: The relational operators are used to test
the relation between two values. All relational operators are binary
operators and therefore require two operands. A relational expression returns
zero when the relation is false and a non-zero when it is true.
Relational Operators |
Meaning |
< |
Less
than |
<= |
Less
than or equal to |
== |
Equal
to |
> |
Greater
than |
>= |
Greater
than or equal to |
! = |
Not
equal to |
3) Logical operators: The logical operators are used to
combine one or more relational expression. The logical operators are
Operators |
Meaning |
|| |
OR |
&& |
AND |
! |
NOT |
AND(&&): evaluates true when both the relations expressions are true.
OR(||) : Evaluates true when any one of the relation expression becomes true.
NOT(!) : this operator reverse the truth value of the expression.
4) Increment & decrement operator: C++ provides two special operators '++' and '--' for incrementing
and decrementing the value of a variable by 1.. Increment and decrement
operators each have two forms, pre and post.
The
syntax of the increment operator is: Pre-increment: ++variable
Post-increment: variable++
The
syntax of the decrement operator is: Pre-decrement: --variable
Post-decrement: variable --
In Prefix form first variable is first incremented /decremented, then
evaluated, In Postfix form first variable is first evaluated, then
incremented/decremented
5) Assignment operator: The assignment operator ' = ' is used for assigning a variable to a value. This operator takes the expression on its right-hand-side and places it into the variable on its left-hand-side.
For example:
m = 5;
The operator takes the expression on the right, 5, and stores it in the variable on the left, m.
x = y = z = 32;
This code stores the value 32 in each of the three variables x, y, and z.
In addition to standard assignment operator shown above, C++ also supports compound assignment operators.
6)
Conditional operator: The conditional operator?: is called ternary operator as it
requires three operands. The format of the conditional operator is:
Conditional_ expression ? expression1 : expression2;
If the value of conditional expression is true then the
expression1 is evaluated, otherwise expression2 is evaluated.
int a = 5, b = 6;
big = (a > b) ? a : b;
The condition evaluates to false,
therefore biggest the value from b and it becomes 6.
7) The comma operator: The comma operator gives left to right
evaluation of expressions. When the set of expressions has to be
evaluated for a value, only the rightmost expression is considered.
int a = 1, b = 2, c = 3, i; // comma acts as separator, not as an
operator i = (a, b); // stores b into i
Would first assign the value of a to i, and then assign value of b to
variable i. So, at the end, variable i would contain the value 2.
8) The sizeof operator : As we know that different types of Variables,
constant, etc. require different amounts of memory to store them The
sizeof() operator can be used to find how many bytes are required for an object
to store in memory.
For example
sizeof (char) returns 1 , sizeof (float) returns 4 .
9) Scope Resolution operator (:: ) It is special operator in
c++ it used in managing variables
and members at outside of active bounders . It enables
-
accessing
global variable
-
setting
and accessing static-member
-
implementing
the member-function
1. It used to access global variable when local variable and
global variables are
same.
:: x
2. It used to set and access static-member at outside of class
with the class name and membername.
int
Karim :: x
3. It used to implement the member-function of a class at outside
of a class
void Karim::show(){ }
10) ‘new’ Operator: The memory allocation operator it allocates
memory dynamically for
specific class-object .
void main
{
Maxi *k=new Maxi();
k->show();
}
The order of Precedence of Arithmetic Operators:
The order in which the Arithmetic operators
(+,-,*,/,%) are used in a. given expression is called the order of precedence.
The following table shows the order of precedence.
First ( )
Second *, /, %
Third +, -
No comments:
Post a Comment