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(if it is defined), from anywhere in a 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.

But, a global version of a variable is created outside any function of a program, i.e. even outside the main() function. Therefore, the scope of a global variable is throughout the program, making it accessible from anywhere in the program.

Now, there could be a situation when we have many versions of a variable, i.e. a global version of a variable and also many local versions of the same variable, where each local version is defined within a different block, such as-

#include<iostream>


using namespace std;

int x =40; //Global version of variable, x, because it is declared and defined outside any function

int main()
{

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

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

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

Looking at the above sample code, we have created a global version of a variable, x, outside the 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). The scope of each local version of the variable x is restricted to its respective block and it cannot be accessed outside its block.

Now, the scope resolution operator comes handy in above described situation as it allows us to access the global version of a variable, x, from anywhere in the program i.e. even from the within the inner blocks where a local copy of a global variable is defined.




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 for accessing the value of a global variable, we have to add the name of a global variable, right 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 an inner block defined in the main() function, where this inner block has declared its own local version of the global variable.

//C++ Example of Scope Resolution Operator


#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


The scope resolution operator is also used to define the class methods, outside the definition of their class, as you may see in the code example below.

#C++ Example of Scope Resolution Operator


#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 the functions of class X, such as - get_x1() and set_x1(), outside the definition of their class.


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

C#, PYTHON and DJANGO


Advertisement