C QUESTIONS What is the purpose of the scope resolution oper
C++ QUESTIONS!!!
What is the purpose of the scope resolution operator?
Solution
Answer:
Scope resolution operator(::) is denoted by ::. Scope resolution operator(::) is used to define a function outside a class and also when we need to use a global variable but also having the same with a local variable.
Example for global variable access by scope resolution operator ::.
#include <iostream>
using namespace std;
int a= 1; // global variable
int main() {
int a = 2; //local variable
cout << \"Local variable: \" << a << endl;
cout << \"Global variable: \" << ::a << endl; //using scope resolution operator
return 0;
}
Output:
sh-4.3$ g++ -std=c++11 -o main *.cpp sh-4.3$ main Local variable: 2 Global variable: 1
Example for mehod call by scope resolution operator ::
#include <iostream>
using namespace std;
class WelcomeMessage{
public:
void welcome(); //function declaration
};
// function definition outside the class
void programming::welcome() {
cout << \"Welcome to c++\"<<endl;
}
