Operators Scope resolution operator(::) in C++ program is typically used to define a function outside a class, or in cases where we want to use a global variable but have a local variable with the same name. This is shown in the example below:
Example:
#include <iostream>
char c = 'a'; // global variable
int main() {
char c = 'b'; //local variable
cout << "Local variable: " << c << "n";
cout << "Global variable: " << ::c << "n"; //using scope resolution operator
return 0;
}
Also, this is an example of a Scope resolution operator in class:
#include <iostream>
class programming {
public:
void output(); //function declaration
};
// function definition outside the class
void programming::output() {
cout << "Function defined outside the class.n";
}
int main() {
programming x;
x.output();
return 0;
}