Special Operator in C++

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

Operator Name                                  Symbol

  1. Scope resolution operators                       ::
  2. Memory allocation operator                    new
  3. Memory release operator                        delete
  4. pointer to member operator                      *
  5.  Line feed operator                                    endl
  6. Field width operator                                 setw


1. Scope resolution Operator

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.

Example program of Scope Resolution Operator :: in C++

Output of Example program of Scope Resolution Operator

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.

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



2  Pointer 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.

Pointer operators
(Address of Operator & , Indirection Operator *)

  • C++ provides two pointer operators, which are Address of Operator & and Indirection Operator *.
  • A pointer is a variable that contains the address of another variable or you can say that a variable that contains the address of another variable is said to “point to” the other variable.
  • A variable can be any data type including an object, structure or again pointer itself.
  • The . (dot) operator and the -> (arrow) operator are used to reference individual members of classes, structures, and unions.
  • The Address of Operator &: The & is a unary operator that returns the memory address of its operand.
  • For example, if var is an integer variable, then &var is its address. This operator has the same precedence and right-to-left associativity as the other unary operators.
  • The Indirection Operator *: The second operator is indirection Operator *, and it is the complement of &. It is a unary operator that returns the value of the variable located at the address specified by its operand.

Example  program of Pointer in C++

Output of pointer in C++

Value of var:555
Value of ptr :0xbff64494
Value of val :555



 3.  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)          Syntax
             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.

Example     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.

 Example   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.

If we want to free a dynamically allocated array, we must use following form.

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

 Syntax  :              delete variable.

Example :            delete [size] variable.

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.



4. Manipulators

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

1) endl

2) setw



1. endl

it is used to insert a linefeed into an output. It has same effect as using .\n. for newline.


int a=2568,n=36,p=275;

cout<<”a=” << a << endl <<”n=” <<n;
cout<< endl<<”p=”<<p<<endl;

output is

a = 2568                n = 36    p = 275


2 setw

With the setw, we can specify a common field width for all the numbers and force them to print with right alignment.


long intr sum= 357;

cout<<setw( 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.

 Output is



3 setprecision

With the setprecision , we can specify a width after decimal point  for all real numbers


cout<<setprecision (2) <<12500.45678<<endl;

Output is


(Visited 1,136 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image