Operator in c++ with example

An operator is a symbol that perform mathematical or logical manipulations. In C++ it have a rich set of c language operators   and other some new operators provide in c++

In C and C++  There areThree types of operators classify in below categories

Unary Operators
Binary Operators
Ternary Operators

 Operator Name Type Of Operator 1. Assignment Operator Unary Operator 2. ShortHand Assignment Operator Unary Operator 3. Arithmetical Operators Binary Operator 4. Relational Operators Binary Operator 5. Logical Operators Binary Operator 6. Conditional Operator Ternary Operator 7. Pre-Fix/Post Fix Unary Operators 8. Bitwise Operators Binary Operator 9. sizeof Operators Binary Operator 10. comma Operators Binary Operator 11. Special Operators Unary Operator

1. Assignment Operator ( = )

A Symbol ‘=’ is used for assignment Operator , it takes the right-hand side (called rvalue) and copy it into the left-hand side (called
lvalue). Assignment operator is the only operator which can be
overloaded but cannot be inherited.

In Example int  a= 10, b=20, c=5;

 Operator Name Operator Symbol Example Result Assignment Operator = c=5 5 Assignment Operator = no=3*5/3-2+3 6 Assignment Operator = x=a+b-c 25

2. ShortHand Assignment Operator +=,  -=,  *=,   /=,   %=

A shorthand operator used when to perform an operation  and assignment at same type notation whenever requirement.
In Example  here  a= 10

 Operator Name Operator Symbol Example same as Result AdditionEquals =+ a+=10 a=a+10 20 SubtractionEquals -= a-=10 a=a-10 0 MultiplicationEquals *= a*=10 a=a*10 100 DivisionEquals /= a/=10 a=a/10 1 ModulusEquals %= a%=10 a=a%10 0

3. Arithmetical  Operators

These type of  operators are perform  mathematical operations. Addition (+) , subtraction (-) , diversion (/) multiplication (*) and modulus (%) are Arithmetical operators. Modulus operator (%) only use with integer type of data means that it cannot be used with floating- point  type of data.

In Example  here  a= 10 and  b=5

 Operator Name Operator Symbol Example Result Addition + c=a+b 15 Subtraction – c=a-b 5 Multiplication * c=a*b 50 Division / c=a/b 2 Modulus % c=a%b 0

4. Relational Operators

These operators establish a relationship between operands. it means that it is an  operator  which compare between two operand and get the result in true or false  boolean  value
Relational operators are :
grater thatn (>), greater than equal  (>=),  less than (<) ,
less than or equal to (<=), equivalent (==) and not equivalent (!=).

In Example  here  a= 10 and b=5

 Operator Name Operator Symbol Example Result Greater Than > c=a>b 1 Greater Than or Equal >= c=a>=b 1 Less Than < c=a

5. Logical Operators

The logical operators are AND (&&) and OR (||) . Logical Operators can be used whenever compare between more then one condition r operands.

If two statement are connected using AND operator, the validity
of both statements will be considered, means if all condition are true thus the result is true .
If two statements  are connected  using OR operator, then either one of them must be valid, means if any one condition among all is true thus the result is true  . generally these operators are used in control statemnts like Decision making branching and looping.

 Operator Name Operator Symbol Example Result Logical AND && m=(a>b)&&(a>c) 1 Logical OR || m=(a>b)||(a>c) 1 Logical NOT ! m= ! (a>b)&&(a>c) 0

6. Conditional  Operator  ? :   ; ( Ternary Operator)

Conditional  Operator  it is a Ternary Operator which is identified by  ? : ;  in which  first expression is always condition the second expression is true and third expression is false A Ternary operator ,it also  can be nested.

Syntax

exp-1 ? exp-2 : exp-3 ;
( condition ) ? exp ( true ) : exp ( false );

whereas exp-1,exp—2,exp-3 are expression.
exp-1 (condition) is evaluated first.
If exp-1  is nonzero(true),the the expression exp-2 is evaluated.
If exp-1 is zero (false) the ,exp-3 is evaluated

Maximum = 6

7. Increment/Decrement Operator (Unary Operator) (++PreFix, – -PostFix)

It is a 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 +.

• Which are unary operator ++ to add by 1 and – – to subtract by 1
• Both are not found in any other language. It is only in C
• We use these statements(++, –) in while, for loop extensively.
• Increment and decrement operators are unary operators and they require variable as their operators.
• It has two types PreFix and PostFix.
The precedence and associatively of ++ and – operators are same as unary + and – .

Difference between Pre-Fix and Post-Fix Operators

 Prefix  (++a, – -b) Postfix  (a++, b- -) Operator Symbol before an Operand Operator Symbol after an operand First  Increment/decrement  and then  value is transfer First value is transfer and when control reach next statement  then increment / decrement ++m(PrefIx Increment) –m(PrefIx Decrement) m++(PostfIx Increment) m–(PostfIx Decrement) Example int m=5, k; K=++m; Result is : M= 6 and k = 6 Example int m=5, k; K=m++; Result is : M= 6 and k = 5 Above example k receive new value. Above example k received old value.

8. Bitwise Operators

These operators are works on bit and bit position
It works with integral data types like char, int and long and
not with floating point values.

Types of Bitwise Operator

Bitwise AND ( &)
Bitwise OR    ( | )
Bitwise XOR (  ^)
Bitwise NOT ( ~)

They can be used as shorthand notation too like
& = , |= , ^= , ~=

 A=15 A 1 1 1 1 B=6 B 0 1 1 0 Bitwise& C=A & B 0 1 1 0 Bitwise| C=A | B 1 1 1 1 Bitwise^ C=A ^ B 1 0 0 1 Bitwise~ C = ~A 0 0 0 0 C = ~B 1 0 0 1

Shift Operators

Shift Operators are used to shift Bits of any variable. It is of
three types,

Left Shift Operator <<
Right Shift Operator >>
Unsigned Right Shift Operator >>>

Example

int A =15 ,B = 6 , C ;

In binary System     A = 1 1 1 1   and  B = 0 1 1 0

Bitwise LeftShift       operand << n

Bitwise RightShift     operand >> n

• These operators are used to move bit patterns either to left or right the left shift operation causes all the bits in the operand op to be shifted to left n position
• In left shift all the bits in operand to be shifted to the left by n position.the leftmost n bits in original bit pattern will be lost and the right most n bit positions that are vacated will be filled with Zero. Same procedure in Right Shift but the difference is the last n Bits off at  right side.

Example
4         << 2     = answer is  16
0000 0100 << 00  means   ( 2 Zeros Fill)  in  the (4) 0100  =
0001  0000 = 16

8 >> 2  = answer is 8

0000 1000 >> 00  means( 2 Zeros OFF) int the (8)1000   =
0000 0010 = 2

9. sizeof operator

• The sizeof is a compile time operator and, used with an operand, it returns the number of bytes the operand occupies.
• The operand may be a variable, a constant or a data type qualifier.
• The sizeof operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer.
• It is also used to allocate memory space dynamically to variables during execution of a program.

Examples m = sizeof (sum);

n = sizeof (long int);
k = sizeof (235L);

cout<<“Size of char      =  ”<<sizeof (char));
cout<<“Size of int         =  ”<<sizeof(int));
cout<<“Size of  float     =  “<<sizeof(float));
cout<<“Size of  double =  “<<sizeof(double));

10. coma operator

• used to link the related expression together. A comma linked list expression are evaluated left to right and the value of right most expression is the value of the combined expression.

Example

Value =  ( x= 10 , y= 5, x+y ) ;

• First assign the value 10 to x ,then 5 to y and finally assign 15 ,
• comma operator has the lowest precendence of  all operators, the parentheses  necessary
• In for loops for(n=1 ,m=10 ; n<=m ; n++ ,m++)
• In while while ( c = getchar ( ), c ! = ‘\0’)

11. Special Operators in C++

(Visited 60 times, 1 visits today)