CPP Scope Resolution Operators

Scope Resolution Operators

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

Example of scope resolution operator

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;
}

Output:

Local variable: b
Global variable: a

Example of scope resolution operator

Example of 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;
}

Output:

Function defined outside the class.


Share This Page to Download:

Share to Download

Share This Page on:


Ask Question