In addition to the operators like arithmetic operators, relational operators, logical operators, the conditional operator and assignment operators that most of the languages support, C++ provides various new operators that are given in Table.
We’ll be covering the following topics in this tutorial:
Scope Resolution Operator (::)
In C++, variables in different blocks or functions can be declared with the same name. That is, the variables in different scope can have the same name. However, a local variable overrides the variables having the same name in the outer block or the variables with global scope. Hence, a global variable or variable in the outer block cannot be accessed inside the inner block. This problem is solved by introducing new operator scope resolution operator :: introduced in C++.
The scope resolution operator : : is a special operator that allows access to a global variable that is hidden by a local variable with the same name. To understand the concept of scope resolution operator, consider this example.
A program to demonstrate the use of scope resolution operator
#include using namespace std; int x = 5; int main () { int x = 3; cout<<”The local variable of outer block is: “<<X; cout<<”\nThe global variable is: “<<::X; int x = 10; cout <<”The local variable of inner block is: “<<X; cout <<”\n The global variable is: “<<::X; } return 0; }
The output of the program is
The local variable of outer block is: 3
The global variable is: 5
The local variable of inner block is: 10
The global variable is: 5
In this example, three variables with same name x are declared. x declared outside main () has global scope, which is hidden by both the variables x declared in the outer block and inner block inside main ( ). The variable x inside the inner block overrides both variables (global variable and the variable declared in outer block).Thus, the global version of variable x is accessed using : : x in the inner and outer block.
Note that the main application of scope resolution operator is in the classes that will be discussed when the classes are introduced.
new and delete: C++ provides two dynamic allocation operators new and delete to allocate and deallocates memory at run-time respectively. The new operator is a unary operator that allocates memory and returns a pointer to the starting of the allocated space. The syntax of allocating memory using the new operator is
p_var = new data_type;
where,
p_ var = the name of a pointer variable new = C++ keyword data_type = "Valid data type of C++"
For example, to dynamically allocate memory to a variable of type int at run-time, a pointer to type int is defined first and then the memory is allocated at run-time using the new operator as shown here.
int *iptr;&amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;amp;nbsp; //pointer declaration ptr = new int; //allocating memory to an int variable
In these statements, the new operator returns the address of the memory allocated for an int variable from the free store and this address is stored in the pointer iptr. The pointer declaration and allocation of memory can also be performed using a single statement as given here.
int *iptr = new inti;
Moreover, allocated memory using new operator can also be initialized. The syntax to initialize the memory using new operator is
p_var = new datatype (value) ;
where, value = initial_value.
For example, consider this statement.
float *fptr = new float (20.45) ; // initializing the newly variable and created variable
Like built-in data types, memory can also be allocated dynamically to derived and user-defined data types like arrays, structures and classes. These examples illustrate this concept.
• To allocate memory to a one-dimensional array, consider these statements.
int *arr_iptr = new int [20] ; //allocating memory space to an array of // 20 integers float *arr_fptr = new float [10]; //allocating memory space to an array of //10 float numbers
• To allocate memory to a multi-dimensional array, consider these statements.
int (*arr_iptr) [3] = new int [3][3]; //allocating memory to a 2-D array int (*arr_iptr) [5][5] = new int [3][5][5]; //allocating memory to a 3-D array
Note that all the dimensions must be specified while creating multi-dimensional arrays with new operator. However, the first dimension can be a variable whose value is provided at run_time. For example, consider these statements.
int (*arr_iptr) [3] [5] new int [] [5] [5]; //invalid int (*arr_iptr) [3] [5] new int [x] [5] [5]; //invalid int (*arr_iptr) [3] [5] new int [3] [5] []; //invalid
The life-time of a variable created at run-time using the new operator is not restricted till the execution of the program. Rather, it remains in the memory until it is explicitly deleted using the delete operator. When a dynamically allocated variable is no longer required, it must be destroyed using the delete operator to ensure safe and efficient use of memory.
The syntax for using the delete operator is
delete p_var;
For example, to delete the pointers iptr and fptr of types int and float respectively, these statements are used.
delete iptr; delete fptr;
Similarly, to delete the array pointers arr_iptr and arr_fptr of types int and float respectively, these statements are used.
delete [] arr_iptr; delete [] arr_fptr;
The function performed by the new and delete operators is similar to the malloc () and free () library functions used in C. However, new and delete operators have several advantages over malloc ()and free ()which are listed here.
• new automatically returns a pointer to the appropriate data type. There is no need to explicitly typecast the pointer as required in malloc() .
• new automatically allocates enough memory to accommodate the object without using the sizeof operator.
• An object can be initialized while allocating space using the new operator.
• Both new and delete operators can be overloaded.
endl and setw: c++ provides various operators that can be used with an output statement to display the formatted output. These operators are known as manipulators. The manipulators which are commonly used are end1 and setw.
The endl manipulator causes a linefeed to be inserted when used with an output statement. The effect of this manipulator is the same as that of the newline character ‘\ n’. To understand the concept of endl, consider this statement.
Cout<<"Name: "<<name<<endl<<"Roll No: "<<rollno<<endl<<"Address: "<<address<<endl;
This statement would display the output in three lines, one for each variable. setw manipulator causes a value to be displayed right-justified within a specified field width. The field width is passed as an argument to this manipulator. To understand the concept of setw, consider these examples.
Example : A program to display the output without using setw
#include using namespace std; int main () { int Refrigerator = 15000, TV=6000, Almirah=800; cout<<”Refrigerator: "<<Refrigerator<<endl; cout<<”TV : "<<Tv<<endl; cout<<”Almirah: "<<Almirah<<endl; return 0; }
The output of this program is
Refrigerator: 15000
TV : 6000
Almirah: 800
In this example, the output is unformatted, which makes it difficult to compare the values. However, this output can be formatted by using setw operator.
Example : A program to display formatted output using setw
#include #include using namespace std; int main () { int Refrigerator = 15000, TV=6000 Almirah=800; cout<<setw(15)<<”Refrigerator:”<<setw(6)<<Refrigerator<<endl; cout<<setw(15)<<”Tv:"<<setw(6)<<TV<<endl; cout<<setw(15)<<”Almirah:”<<setw(6)<<Almirah<<endl; return 0; }
The output of the program is
Refrigerator:15000
TV:6000
Almirah:800
In this example, setw(15) specifies the field width 15 for displaying the strings and setw(6) specifies the-field width 6 for displaying the value of variables. The values are right -justified within the specified field width.
Operators Precedence
Generally, an expression consists of more than one operator; hence, the compiler needs to know which operator is to be evaluated first. For this, it is important to determine the precedence and associatively of operators. The order in which different operators in an expression are evaluated is determined by the precedence of operators. The operators with a higher precedence are evaluated before the operators with a lower precedence. However, the order in which operators of the same precedence are evaluated is determined by the associatively of operators. The associatively of an operator can be either from the left to the right or from the right to the left. The operators with the left to right associatively are evaluated from the left -hand side while the operators with the right to left associatively are evaluated from the right -hand side.
The precedence and associatively of the C++ operators are listed in Table. Note that the precedence of operators decreases from top to bottom.