Ticker

6/recent/ticker-posts

OPERATORS IN C++ WITH EXAMPLE - C++ Programming

OPERATORS IN C++

C++ had rich set of operators. Some of the new operators in C++ are- 

  1. :: - Scope resolution operators. 
  2. ::* - pointer to member decelerator. 
  3. →*-pointer to member operator. 
  4. .* - pointer to member operator. 
  5. delete - memory release operator. 
  6. endl - Line feed operator 
  7. new -Memory allocation operator. 
  8. stew - Field width operator.

Scope resolution Operator. 

1) C++ is a block - structured language. The scope of the variable extends from the point of its declaration till the end of the block containing the declaration.

2) Consider following program. 

........................

...................

{ int x = 1; 

    === 

}

===== 

{ int x = 2; 

} ===== 

The two declaration of x refer to two different memory locations containing different values. Blocks in c++ are often nested. Declaration in a inner block hides a declaration of the same variable in an outer block. 

3) In C, the global version of a variable cannot be accessed from within the inner block. C++ resolves this problem by using scope resolution operator (::), because this operator allows access to the global version of a variable.

4) Consider following program. 

# include<iostream.h >

int m = 10;

int main ( )

{

int m = 20;

{

int k = m;

int m = 30;

cout << ìK = ì <<k;

cout << ìm = ì<<m;

cout << : : m = ì << : : m;

}

cout << ìmî = ì<< m;

cout << ì::m = <<::m;

}

return 0;

}

The output of program is

k = 20

m = 30

:: m = 10

m = 20

:: m =10

In the above program m is declared at three places. And ::m

will always refer to global m.

5) A major application of the scope resolution operator is in the classes to identify the class to which a member functions belongs.

Member dereferencing operators - 

C++ permits us to define a class containing various types of data & functions as members. C++ also permits us to access the class members through pointers. C++ provides a set of three pointer. C++ provides a set of three pointers to member operators. 

1) ::* - To access a pointer to a member of a class. 

2) .* - To access a member using object name & a pointer to that member. 

3) →* - To access a member using a pointer in the object & a pointer to the member.

Memory management operators. 

1) We use dynamic allocation techniques when it is not known in advance how much of memory space is needed. C++ supports two unary operators new and delete that perform the task of allocating & freeing the memory. 

2) An object can be created by using new and destroyed by using delete. A data object created inside a block with new, will remain existence until it is explicitly destroyed by using delete. 

3) It takes following form. variable = new data type The new operator allocated sufficient memory to hold a data object of type data-type & returns the address of the object. 

    EX - p = new int. 

    Where p is a pointer of type int. Here p must have already been declared as pointer of appropriate types. 

4) New can be used to create a memory space for any data type including user defined type such all array, classes etc. 

        Ex- int * p = new int [10] 

        Creates a memory space for an array of 10 integers. 

5) When a data object is no longer needed, it is destroyed to release the memory space for reuse. The general form is delete variable. If we want to free a dynamically allocated array, we must use following form. 

        delete [size] variable. 

The size specifies the no of elements in the array to be freed. 

6) The new operator has following advantages over the function malloc() in c -. 

    i) It automatically computes the size of the data object. No need to use sizeOf().

    ii) If automatically returns the correct pointer type, so that there is no need to use a type cast. 

    iii) new and delete operators can be overloaded. 

    iv) It is possible to initialize the object while creating the memory space

Manipulators

Manipulators are operators that are used to format the data display. There

are two important manipulators. 

1) endl

2) stew

1) endl : - This manipulator is used to insert a linefeed into an

output. It has same effect as using ì\nî for newline. Ex- cout<< ìaî << a << endl <<în=î <<n;

<< endl<<îp=î<<p<<endl;

The output is

a = 2568

n = 34

p = 275

2) Setw : With the stew, we can specify a common field width for

all the numbers and force them to print with right alignment. EX ñ cout<<stew (5) <<sum<<endl;

The manipulator setw(5) specifies a field width of 5 for printing the value of variable sum the value is right justified.

Type cast operator. 

C++ permits explicit type conversion of variables or expressions using the type cast operator. 

Syntax - type name (expression) 

Ex ñ avg = sum/float(i) 

Here a type name behaves as if it is a function for converting values to a designated type.

ALSO SEARCH:

"types of operators in C++"

"logical operators in C++"

"operators in C++ "

"operators in C++ with example program"

"bitwise operators in C++"

"arithmetic operators in C++"

"types of operators in C++"

"logical operators in C++"

"operators in C++ with example program"

"assignment operators in C++"

"relational operators in C++"