Scope Resolution Operator in C++

This section will discuss the scope resolution operator and its various uses in the C++ programming language. The scope resolution operator is used to reference the global variable or member function that is out of scope. Therefore, we use the scope resolution operator to access the hidden variable or function of a program. The operator is represented as the double colon (::) symbol.

Scope Resolution Operator in C++

For example, when the global and local variable or function has the same name in a program, and when we call the variable, by default it only accesses the inner or local variable without calling the global variable. In this way, it hides the global variable or function. To overcome this situation, we use the scope resolution operator to fetch a program's hidden variable or function.

Uses of the scope resolution Operator

  1. It is used to access the hidden variables or member functions of a program.
  2. It defines the member function outside of the class using the scope resolution.
  3. It is used to access the static variable and static function of a class.
  4. The scope resolution operator is used to override function in the Inheritance.

Program to access the hidden value using the scope resolution (::) operator

Program1.cpp

Output

The value of the local variable num: 100
 The value of the global variable num: 50

Program to define the member function outside of the class using the scope resolution (::) operator

Program2.cpp

Output

It is the member function of the class.

Program to demonstrate the standard namespace using the scope resolution (::) operator

Program3.cpp

Output

Enter the value of num: 50
 The value of num is: 50

Program to access the static variable using the scope resolution (::) operator

Program4.cpp

Output

The value of the static integer n1: 5
 The value of the local variable n1: 15
 The value of the Base::n2 = 10

Program to access the static member function using the scope resolution (::) operator

Program5.cpp

Output

Use scope resolution operator to access the static member.

Program to override the member function using the scope resolution (::) operator

Program5.cpp

Output

 It is the test() function of the ABC class.
 It is the test() function of the child class.