Example & Tutorial understanding programming in easy ways.

Define Scope resolution operator in C++

Scope resolution operator (::)
In C++ programming language, it 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 the same name.

Syntax with function:
retrun_Type class_name :: function_name()
{
}

Syntax to access global variable:
::global_varible

Scope resolution operator with global variable:

program:

#include< iostream>
using namespace std;
//global variable
char c = 'a';
int main(){
//local variable
char c = 'b';
cout << "Local variable: " << c << "n";
//using scope resolution operator
cout << "Global variable: " << ::c << "n";
return 0;
}


output-

Local variable: b
Global variable: a


Scope resolution operator with function:

program:

#include< iostream>

using namespace std;
class Myclass
{
public:
void function();
};
void Myclass::function()
{
cout<<"This is my function outside class";
}
int main()
{
Myclass obj;
obj.function();
return 0;
}


output-

This is my function outside class




Read More →