/    /  CPP – Scope resolution operator in C++

Scope resolution operator in C++

 

Scope resolution operator(::) is used to define a function outside of a class or when we wish to use a global variable but also has a local variable with the same name. 

 

Example Program:

#include <iostream> 
using namespace std;  
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;
}

 

Output:

 

Scope resolution operator in class

Example Program:

#include <iostream>
using namespace std;
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;
}

Output: