Scope Resolution operator in C++ – 4 variants to know!

Filed Under: C++
C Scope Resolution Operator

Hey, folks! In this article, we will be focusing on 4 Variants of Scope resolution operator in C++.


What is Scope resolution operator in C++?

C++ programming language, comprises of various operators to deal and manipulate the data. One such operator is Scope resolution operator(::).

Scope resolution operator serves the following purpose:

  • Access a global data variable when both the global and the local variable share the same name/label.
  • Enable us to define a member function outside a class.
  • Referring to a class inside another class.
  • To access the static variables of a class.

4 Variants of Scope Resolution Operator

Let us now try to access a global variable using Scope resolution operator as shown:

1. Accessing global variables when local variable shares the same name

When both the global and local variables have the same name, we can access the global variable using the scope resolution operator as shown below.

Syntax:

::variable-name;

Example:

#include<iostream> 
using namespace std; 

int var = 100; //Global/class data member

int main() 
{ 
int var = 1; // Local data member 
cout << "Global variable--var: " << ::var; 
return 0; 
} 

Output:

Global variable--var: 100

Further, we will try to define a member function outside the class as shown below using scope resolution operator–

2. Defining a function outside a class using :: operator

Syntax:

return_type Class::function()
{}

Example:

#include<iostream> 
using namespace std; 

class Scope 
{ 
    public: 
    void show(); 
}; 

//Function definition outside the class using :: operator
void Scope::show() 
{ 
    cout<<"Welcome to JournalDev!!"; 
} 

int main() 
{ 
    Scope SC;
    SC.show();
    return 0; 
} 

In C++, we can define functions outside the class using :: operator as shown above.

Output:

Welcome to JournalDev!!

Now, will be using Scope resolution operator along with namespace in C++.

3. Scope resolution operator with standard namespace

#include<iostream> 

int main()
{   int x =0;
	std::cout<<"Enter the value for x\n";
	std::cin>>x;
	std::cout<<"Value of x: "<<x;
} 

In cases where two different namespaces have the same class name, the :: operator can be used to distinguish them without an error as shown above. So, here, cin and cout belong to the standard namespace.

Output:

Enter the value for x
10
Value of x: 10

4. Accessing static data members of a class using scope resolution operator

#include<iostream> 
using namespace std; 

class static_scope 
{ 
public: 
	static int var; 
	void show(int var) 
	{ 
	cout << "Static variable--var: " << static_scope::var<<endl; 
	cout << "Local variable--var: " <<var; 
	} 
}; 

int static_scope::var = 100; 

int main() 
{ 
	static_scope S; 
    int var = 20; 
	S.show(var); 

	return 0; 
} 

Here, we have accessed the static data member ‘var’ using scope resolution operator as shown below.

Output:

Static variable--var: 100
Local variable--var: 20

Conclusion

By this, we have come to the end of this topic. Hope this article helps you to understand the concept well!

Feel free to comment below, in case you come across any question.

Till then, Stay tuned @ C++ programming with JournalDev and Keep Learning!!


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages