Wednesday, 10 August 2016

Operators in c++

                                                            Operators In C++

Operators are special type of functions,that takes one or more argument and produces a new value. For example: addition(+) , subtraction (-), multiplication (*), etc,are all operators . Operators are used to perform various operations on variables and constants .




                                                         TYPES OF OPERATORS 


  1. Assignment operators
  2. Mathematical operators 
  3. Relational operators 
  4. Logical operators
  5. Bitwise operators 
  6. Shift operators
  7. Unary operators 
  8. Ternary operators 
  9. Comma operators




                                                      ASSIGNMENT OPERATORS

Operator "=" is used for assignment , 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 .

 RESULT OF ASSIGNMENT OPERATORS:

The assignment operators return the value of the object specified by the left operand after the assignment. The resultant type is the type of the left operand. The result of an assignment expression is always an l-value these operators have right- to- left associativity . the left operand must be a modified l-value.

In ANSI C , the result of  an assignment expression is not an l-value . therefore ,the legal c++ expression (a+=b)+=c is illegal .

                                                    MATHEMATICAL OPERATORS

There are operators used to perform basic mathematical operations . Addition (+),subtraction(-),
division(/),multiplication(*) and modulus(%) are the basic mathematical operators.Modulus operator cannot be used with floating -point numbers.
C++ and C also use a shorthand notation to perform an operation and assignment at same type .Example,

int x=10 ;
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 

A relational operators compares two operands to determine whether one is greater than ,greater than or equal to , less than, less than or equal to the other:
> greater than
< less than
>= greater than equal to
<= less than equal to
When used in an expression they all return a boolean value which states the result of the comparison (i.e, 4>3 can be read as is 4 greater than 3 ?which returns true )

EXAMPLE:

int x=12;     //assignment operator
x=5;           // again assignment operator
if(x==5)    // here we have used equivalent relational operator , for comparison
{
cout<< " successfully compared";
}

                                                 LOGICAL OPERATORS

The logical operators apply logic functions (NOT,AND, and inclusive OR) to boolean arguments (or types  contextually -convertible to bool),with a boolean result Unlike the bitwise logic operators ,these operators do not evaluate the second operand if the result is known after evaluating the first .
                                                                     OR
The logical operators are AND(&&) and OR(||). They are used to combine two different expression together .
If two statement are connected using AND operator, the validity of both statement 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 and in decision making .

                                                 BITWISE OPERATORS

The bitwise operators performs bitwise -AND(&),bitwise-exclusive-OR(^),and bitwise -inclusive-OR(|) operations.
The operands of bitwise operators must have integral types, but their types can be different . These operators perform the usual arithmatic conversions:the type  of the result is the type of the operands after conversion .

                                                    SHIFT OPERATORS 

Shift operators are used to shift bits of any variable .ITis of three types

  1. Left  shift operator <<
  2. Right shift operator >>
  3. Unsigned right shift operator >>>
                                                    UNARY OPERATORS

A unary operator in c# is an operator that takes a single operand in an expression or a statement . The unary operators in c#  are +,-,!,~,++ and the  cast operator .
other unary operators : address of &,dereference *,new and delete,bitwise not ~,logical not !, unary minus -, and unary plus +.
                                                     TERNARY OPERATORS 

The ternary if-else ?: is an operator which has three operands .
int a=11;
a>6? cout<<"true":cout<<"false";

                                                      COMMA OPERATORS

This is used to separate variable names and to separate expression .In case of expression, the value of last expression is produced and used .

EXAMPLE:

int a,b,c;             //  variable declaration using comma operator
a=b++,c++          // a=c++  be done .

                                                         SIZE OF OPERATOR IN C ++

Sizeof is also an operator not a function , it is used to get information about the amount of memory allocated for data types & objects . It can be used to get size of user defined data types too.

sizeof operator can be used with and without parentheses. i you apply it to a variable you can use it without parentheses.

cout<<sizeof(double); // will print size of double
int x=2;
int i=sizeof x;

No comments:

Post a Comment