C++ had rich set of operators. Some of the new operators in c++ are
Operator Name Symbol
- Scope resolution operators ::
- Memory allocation operator new
- Memory release operator delete
- pointer to member operator *
- Line feed operator endl
- 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++
int m = 10; // Global Variable
void main ()
int m = 20; // Global Variable Overridden
int k = m;
int m = 30; // Aagain override Global variable
cout <<"K = "<<k<<endl; // Get local value 20
cout <<"m = "<<m<<endl; // Get overridden value 30
cout <<"::m = "<<::m<<endl; //Get original global value 10
cout <<"m = "<<m<<endl; // Get overridden value 20
cout <<"::m = "<<::m<<endl; // Get original global value 10
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.
(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++
ptr = &var; // take the address of var
val =*ptr; // take the value available at ptr
cout <<"Value of ptr :"<< ptr << endl;
cout <<"Value of var :"<<var<< endl;
cout <<"Value of val :"<< val << endl;
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.
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 
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.
Manipulators are operators that are used to format the data display. There are two important manipulators.
it is used to insert a linefeed into an output. It has same effect as using .\n. for newline.
cout<<”a=” << a << endl <<”n=” <<n;
a = 2568 n = 36 p = 275
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;
The manipulator setw(5) specifies a field width of 5 for printing the value of variable sum the value is right justified.
With the setprecision , we can specify a width after decimal point for all real numbers
cout<<setprecision (2) <<12500.45678<<endl;