As the name suggests, Scope resolution operator is used to get the hidden names due to variable scopes so that you can still use them. In this article we will understand how to use scope resolution operator in C++ & what are its different purposes from a programming perspective.
In C++, scope resolution operator is ::. Scope resolution operator in C++ can be used for:
- Accessing a global variable when there is a local variable with same name
- Defining a function outside a class
- Accessing a class’s static variables
- Referring to a class inside another class
- In case of multiple Inheritance
- Namespace
Now let’s understand each of the purpose one by one with the help of examples.
Accessing a global variable when there is a local variable with same name
You can use scope resolution operator to access the global variable if you have a local variable with the same name. In the below example we have two variables both name num with global & local scope. So, to access the global num variable in the main class you need to use scope resolution operator (i.e. ::num).
Example
#include<iostream> using namespace std; int num = 30; // Initializing a global variable num int main() { int num = 10; // Initializing the local variable num cout << "Value of global num is " << ::num; cout << "nValue of local num is " << num; return 0; }
Output
Moving on with this article on Scope Resolution Operator In C++
Defining a Function Outside a Class
If you are declaring a function in a class and then later want to define it outside the class, you can do that using scope resolution operator. In the below example, we are declaring a function Speed in Class Bike. Later we are defining the function in the main class using scope resolution operator.
Example
#include<iostream> using namespace std; class Bike { public: // Just the Function Declaration void Speed(); }; // Defining the Speed function outside Bike class using :: void Bike::Speed() { cout << "Speed of Bike is 90 KMPH"; } int main() { Bike bike; bike.Speed(); return 0; }
Output
Moving on with this article on Scope Resolution Operator In C++
Accessing a class’s static variables
You can access the class’s static variable using class name & scope resolution operator (i.e. class_name::static_variable). You can see in the below example, we are declaring a static variable in the class. We are defining the variable outside the class using the scope resolution operator. Then we are accessing it using class name & scope resolution operator.
Example
#include<iostream> using namespace std; class Try { static int num1; public: static int num2; // Local parameter hides class member // Class member can be accessed it using :: void function(int num1) { // num1 static variable can be accessed using :: // inspite of local variable num1 cout << "Static num1: " << Try::num1; cout << "nLocal num1: " << num1; } }; // Defining a static members explicitly using :: int Try::num1 = 10; int Try::num2 = 15; int main() { Try o; int num1 = 20 ; o.function(num1); cout << "nTry::num2 = " << Try::num2; return 0; }
Output
Moving on with this article on Scope Resolution Operator In C++
Referring to a class inside another class
You can create nested class with same variable names in both the classes. You can access both the variables using scope resolution operator. For the inner class variable, you need to use Outer_Class::Inner_Class::variable.
Example
#include<iostream> using namespace std; class Outside_class { public: int num; class Inside_class { public: int num; static int x; }; }; int Outside_class::Inside_class::x = 5; int main(){ Outside_class A; Outside_class::Inside_class B; }
Moving on with this article on Scope Resolution Operator In C++
In case of multiple Inheritance
If you have two parent classes with same variable names and you are inheriting both of them in the child class, then you can use scope resolution operator with the class name to access the individual variables.
In the below example, we are creating two parent class Parent1 & Parent2, and both of them have variable num. When we are inheriting both of them in Child class, we can access both the num variables using class name & scope resolution operator.
Example
#include<iostream> using namespace std; class Parent1 { protected: int num; public: Parent1() { num = 100; } }; class Parent2 { protected: int num; public: Parent2() { num = 200; } }; class Child: public Parent1, public Parent2 { public: void function() { cout << "Parent1's num is " << Parent1::num; cout << "nParent2's num is " << Parent2::num; } }; int main() { Child obj; obj.function(); return 0; }
Output
Moving on with this article on Scope Resolution Operator In C++
Namespace
Suppose we have two namespaces & both contains class with same name. So to avoid any conflict we can use namespace name with the scope resolution operator. In the below example we are using std::cout.
Example
#include<iostream> int main(){ std::cout << "Hello" << std::endl; }
Output
Now after going through the above programs you would have understood everything about scope resolution operator in C++. I hope this blog is informative and added value to you.
Now after executing the above program you would have understood the Scope Resolution Operator In C++. Thus we have come to an end of this article on ‘Quicksort in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.