Scope resolution operator

The scope resolution operator (: : ) in C++ is used to define the declared member functions (in the header file with cpp extension) of the in the cpp file one can define the normal functions or the number functions of the class. To differentiate from the normal functions with the member functions of the class, one needs to the use the scope resolution operator (: : ) in between the class name and the member function name, ship : : foo () where the ship is class and the foo () is member function in the ship. The other applications of the resolution operator is to resolve the scope of the variables if the identical variable name is used for local, global, and the data member of the class. If the resolution operator is placed between the class name and the data member belonging to the class than data name belonging to the particular class is affected. If the resolution operator is placed in front of the variable name than the global variable is affected. If no resolution operator is placed then the global the local variable is affected. You can inform the compiler to utilize the global identifier rather than local identifier through prefixing the identifier with : : the scope resolution operator.

: : identifier

Class name : : identifier

Name space : : identifier // the identifier can be a variable or a function.

If we have nested local scopes, the scope resolution operator does not provide access to identifier in the next outer most scope. It gives access to only the universal identifiers. This instance has two variables namely amount. The first one is global and consists of the value 123. The second is restricted to the main function. The scope resolution operator states the compiler to utilize the global amount in spite of the local one.

# include < iostream. h >

Using name space std;

Int amount = 123; // a global variable

Int main ()

{

Int amount = 456; // a local variable

Cout << : : amount << end l; // print the global variable

   Cout << : : amount << end l; // print the local variable

                 }

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.