Advertisement



< Prev
Next >



C++ Scope Resolution Operator




C++ provides us many important operators and out of all, we are going to discuss the scope resolution operator. The scope resolution operator allows us to access the global version of a variable, from anywhere in the program. For those who don't know what the global version of a variable is, please continue reading.

When a variable/function is declared within a block, it can only we accessed from within the same block and not outside it. Hence, the scope of a variable/function in which it can be accessed, starts from the opening brace { of the block in which it is defined, and ends at the closing brace } of the same block.

There could be many versions of a variable having the same name, where each version of a variable is declared within a different block and all versions exist with a global version as well, such as -

int main()
{
int x =40; //Global version of variable, a
{

	{
		int x = 10; //Local version of the variable, a
	}	

	{
		int x = 20; //Local version of the variable, a 
	}

	{ 
		int x = 30; //Local version of the variable, a
	}
}

Looking at the above sample code, we have declared a global variable of a variable a within the scope of main() function and there are also three local versions of the variable x(all declared within the three different inner blocks in the main() function), where the scope of each version is restricted to its respective block and cannot be accessed outside it.

Now, the scope resolution operator comes into the picture as it allows us to access the global version of a variable, from anywhere in the program or within any block defined in the program.




Syntax of scope resolution operator


The scope resolution operator is denoted by the symbol ::



As you may see in the picture above, to use the scope resolution operator to access the value of a global variable,m from anywhere in the program, we have to add the name of the global variable after the scope resolution operator ::



Example of Scope Resolution Operator


Let us take a look at an example where we are using the scope resolution operator :: to access the value of global version of a variable, from within any inner block defined in the main() function, where each inner block has declared its own local version of the global variable.

#include<iostream>

using namespace std;

int a = 10; 		//Global version of variable, a

int main()
{
int a = 20;		//First local version of variable, a

	{
	int a =30;	//Second local version of variable, a
	cout<<"The value of second local version of a : " << a <<"\n";
	cout<<"The value of global version of a : " << ::a << "\n"; ;
	}
	
cout<< "The value of first local version of a : " << a << "\n";
cout<< "The value of global version of a : "<< ::a;
} 


Output

The value of second local version of a : 30
The value of global version of a : 10
The value of first local version of a : 20
The value of global version of a : 10



Program Analysis





Advertisement




Another use of Scope Resolution Operator


Another use of scope resolution operator is used to define class methods, as you may see in the code example below.

#include<iostream>

using namespace std;

class X
{
private :
 int x1;
 
public :
 int get_x1();
 void set_x1();
};



//Using Scope Resolution Operator to define get_x1() function of X class
int X :: get_x1()
{
	return x1; 
}


//Using Scope Resolution Operator to define set_x1() function of X class
void X :: set_x1(int x)
{
	x1 = x; 
}



int main()
{
X ob;
ob.set_x1(10);
cout << "The value of x is : " << ob.get_x1(10);
}


Output

The value of x is : 10


As you can see in the program, we have used the scope resolution operator to define functions of class, such as - get_x1() and set_x1() function.


Please share this article -




< Prev
Next >
< Conditional Operator in C++
Type Cast Operator >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement