Translate

Thursday 10 May 2012

Operators in C++



Arithmatic operators:- These operators are used to carry out simple arithmetic operations such as addition, subtraction, multiplication, division etc.

+             addition
-              subtraction
/              division
                       *              multiplication
                       %             remainders
                Example:             14%5=4                 14%(-5)=4                  5%14=5             (-14)%5=-4          
Thus the sign of the numerator determines the size of remainder.

             a)      Assignment operators:- The C++ uses symbol ‘=’ as an identifier.
                Example:             a=10;
                                           const int b=5;
             b)       Unary operator:- This operator always preceeds a value. Tha symbol – is generally used as unary             ‘-‘ as shown below.
x=-y;
a=a-(-b);

Increment and decrement operator:- The C++ facilitates use of two new operators ‘++’ and ‘--‘ .  The ‘++’ operator increments value of a variable by 1 while – decrements the value of a variable  by 1.
Example:             ++a;   a++;    => a=a+1;
                                 --a;     a--;    => a=a-1;
let a=6
i) x=++a;              =>    x=7 and a=7
ii) x=a++;   =>    x=6 and a=7
In i) the value of a is incremented by 1 i.e. a becomes 7 and then x is assigned with a new value which is known as preincrement and operator used is called as preincrement operator.
In ii) the value of a is assigned to x before incrementing a which is known as postincrement and operator used is called as postincrement operator.

Relational operator:- Operators of this type are used to compare two values for finding relation between the values. For instance:- to find maximum of two numbers we have to use <,> operators.
>             greater than
<             less than
>=           greater than or equal to
<=           less than or equal to
==           to check equality
!=            not equal to
                In c++ the relational operator returns one when condition is true otherwise zero.

Logical operator:- These operators can be used to connect two or more relational expressions or to find zero or non zero value assigned with a variable. The C++ uses three logical operators.
&&         Logical AND
||           Logical OR
!              Logical NOT
The first two operators can always be used between two values or relations while the third always precedes a value or a relation. Logical operators treat non zero values as ‘1’ and zero values as ‘0’.

Conditional Operator:- It is also known as ternary operator. It uses symbols ‘?’ and ‘:’ to form an operator.
Syntax:                 condition ? statement1 : statement2;
big= a>b ? a : b;
If value of a is greater than b then big=a otherwise big=b.

Bitwise operator:- These operators can be used to perform operations on binary digits known as bits. They include
                                                        I.            Bitwise AND(&):- This operator performs operation on two bits and provides bit ‘1’ as output if both the bits are high(1) otherwise ‘0’.
                                                      II.            Bitwise OR(|):- This operator performs operation on two bits and provides bit ‘1’ as output when atleast one of the bits is high(1) otherwise ‘0’.
                                                    III.            Bitwise Ex-OR(^):- This operator performs operation on two bits and provides bit ‘1’ as output when both the bits are distinct otherwise ‘0’.
                                                    IV.            Bitwise 1’s complement(~):- It performs operation on one bit and provides output ‘1’ for input ‘0’ and vice versa. This is also known as bitwise NOT operator.
                                                      V.            Shift right operator(>>):- This operator shifts the binary bits of a number towards right and fills the leftmost blank position with ‘0’.
                                                    VI.             Shift left operator(>>):- This operator shifts the binary bits of a number towards left and fills the rightmost blank position with ‘0’.

Shorthand operators:- The arithematic and bitwise operators can be used as short hand operators as illustrated below.
a=a+b;      =>       a+=b;
a=a-b;       =>       a-=b;
a=a*b;      =>       a*=b;
a=a^b;      =>       a^=b;

Special operators:- The C++ supports a large number of special operators. Few of them are listed below.
                                                                    I.            Coma(,):- It is used to separate a list of variable in variable declaration.
                                                                  II.            &:- It is known as address operator or reference operator. It is used to provide a duplicate name or to assign address to a new variable.
                                                                III.            *:- This is used to declare a pointer variable of type int, float etc.
                                                                IV.            sizeof():- This operator provides or display the size of a datatype in bytes. Remember sizeof() is not a function.
                                                                  V.            Casting operator:- It is used to convert one type of data into another type while executing a statement, temporarily.
int a=10,m=3;
a/m=3;
(float)a/m=3.333333;
a/(float)m=3.333333;
(float)(a)/m=3.333333;
                                                                VI.            New:- This operator is used to allocate memory space or storage space for a pointer variable.
int *a;
a= new int[10];
                                                              VII.            delete:- This operator is used to release memory space allocated to the pointer variable.
                                                            VIII.            Scope resolution operator(::):- This operator plays vital role in OOPs while expanding or defining function of a class calling a static member function and also helps us to process a global variable in functions.


No comments:

Post a Comment

Total Pageviews