5. CPP-Operators

CPP-Programming Examples:Previous                                          Next: CPP Functions

Operators like arithmetic, relation, logical works same as works in C. Learn Here .
But CPP also provides some different operators from C.
Following is list of other Operators:
  1. [::] scope resolution operator.
  2. [delete] memory release operator.
  3. [endl] line feed operator.
  4. [new] memory allocation
  5. [setw] field width operator

1. Scope resolution operator :


  • The notation of scope resolution is :: .
  • This operator is used for two purposes:
  • This operator tells compiler that the specified function belongs to which class or we can define the definition of member function at outside the class.
Syntax :
Classname ::functionname( )   
This operator is used to access the global variable.
For eg:
intabc;
void main( )
{
void func( );
abc=1;
cout<<” value of abc is = ”<
func( );
}
void func( )
{
intabc;
abc=4;
::abc++;
cout<<”local variable is =”<
cout<<”global variable is=”<<::abc span="">
}
2. Memory release operator and 4. Memory allocation :


  • Both memory allocation and release operator is known as memory management operator which are used to allocate or deallocate memory occupied by the object.
  • For memory allocation ‘new’ is used.
  • Syntax :
Pointer_variable = new datatype;
Eg :
int *ptr;
ptr=new int;
  • new’ keyword can be used to create a memory space for arrays ,classes and structure also.
Eg:
int * array_pointer = new int[2][3];

  • For deallocation the memory occupied by new operator delete operator is used.
  • Syntax :
delete pointer_variable;
Eg:
delete ptr;
3. Line feed operator and 5. Field width operator (setw):


  • The line feed and field width operators are known as manupilators.
  • The line feed operator (endl) and field width (setw) are used to set the format of displayed data.
  • endl is used to insert a new line.
For eg:
cout<<”my name is Seeta“<
cout<<”I live in Rajasthan”;
output :
my name is Seeta
I live in Rajasthan
5. setw:

It is used to specifies a field width for printing value of a variable and also knwown as field width operator.
syntax :
setw(width);
width is numeric value.

CPP-Programming Examples:Previous                                          Next: CPP Functions