Scope Resolution Operator in C++



In C++, scope resolution operator is used to define a function outside the class and access the static variables of a class. It accesses the identifiers such as classes and functions and is denoted by double colon (::).

Here is the syntax of a scope resolution operator −

scope_name :: identifier

Here −

  • scope − It can be a class name, namespace, or, global.
  • identifier − The identifier can be a variable, function, type, or, constant.

Read this chapter to get a better understanding of various applications of scope resolution operator.

Application of Scope Resolution Operator

The scope resolution operator is used for multiple purposes that are mentioned below −

namespace Resolution

A namespace is used to differentiate similar functions, classes, variables, etc., with the same name available in different libraries. To access members which are defined inside a namespace, we use the scope resolution operator '::'.

In this example, we have used the scope resolution operator with cout to print the message.

#include <iostream>
int main() {
   std::cout << "This message is printed using std::" 
   << std::endl;
}

The output of the above code is as follows −

This message is printed using std::

Defining a Function Outside Class

To define a function outside the class, we use the scope resolution operator. It is used to differentiate a global function from a function that belongs to a class. Here is an example −

In this example, we have declared a function display in the class Example. To define this function outside the class, we have used the scope resolution operator. This function displays the value of the num.

#include <iostream>
using namespace std;
class Example
{
   int num = 10;
public:
   void display();
};
void Example::display() // Defining Function outside class
{
   cout << "The value of num is: " << num;
}
int main()
{
   Example obj;
   obj.display();
   return 0;
}

The output of the above code is as follows −

The value of num is: 10

Accessing Global Variable

To access a global variable that is already locally present in a function, we use the scope resolution operator with the global variable.

The following example prints the value stored inside the variable num. The 'num' variable is used as local as well as a global variable, and we have used the scope resolution operator to access the global variable.

#include<iostream>
using namespace std;
int num = 7;
int main() {
   int num = 3;
   cout << "Value of local variable num is: " << num;
   cout << "\nValue of global variable num is: " << ::num;
   return 0;
}

The output of the above code is as follows −

Value of local variable num is: 3
Value of global variable num is: 7

Accessing static Member Variables

To access or define a static member variable, we use a scope resolution operator. The static members belongs to class and are object-independent so they must be defined outside the class. They can be accessed using a class name with the help of the scope resolution operator.

Here is an example to define and access the static member variable name. We have used a class with a scope resolution operator to access the static variable −

#include <iostream>
using namespace std;

class Company {
   public:
   static string name;  //Declaring static variable
};

string Company::name = "Tutorials Point";  //Defining static variable

int main() {
    //Accessing static variable using Scope Resolution Operator 
   cout << "Welcome to " << Company::name << endl; 
   return 0;
}

The output of the above code is as follows −

Welcome to Tutorials Point

Iterator Declaration

An iterator in C++ is used with container like vector, list, map etc. to traverse, access, and modify the container elements. To declare an iterator, we must use the scope resolution operator (::) with the container name.

In this example, we have used an iterator it to traverse and print the vector elements using (::).

#include <iostream>
#include <vector>
using namespace std;

int main(){
     vector<int> numbers = {10, 20, 30, 40, 50};

     // Declaring iterator using scope resolution operator
     vector<int>::iterator it;

     cout << "Given vector: ";
     for (it = numbers.begin(); it != numbers.end(); ++it) {
       cout << *it << " ";
     }

     return 0;
}

The output of the above code is as follows −

Given vector: 10 20 30 40 50

Calling Parent Class Function in Overriding

The function overriding allows a derived or child class to redefine a function that is already defined in the base or parent class. We use the scope resolution operator when we want to access the function of the parent class that is also present in the child class using the child class object.

In this example, we have used the child object to print the output of the parent class function using the scope resolution operator.

#include <iostream>
using namespace std;

class Parent {
public:
   void show() {
      cout << "This is Parent class show() function" << endl;
   }
};

class Child : public Parent {
   public:
   void show() {
      cout << "This is Child class show() function" << endl;
    }
};

int main() {
    Child child1;
    child1.show();
    child1.Parent::show();  // Calling parent class function using ::
}

The output of the above code is as follows −

This is Child class show() function
This is Parent class show() function

Demonstrating Inheritance

The scope resolution operator helps you to access base class members from a derived class in inheritance. You can use scope resolution operator in single and multiple inheritance but it serves different purposes in both conditions. The following examples explain different uses of scope resolution operator in single and multiple inheritance:

Example: Single Inheritance

In this example, we have used the scope resolution operator to access the base class function display() from the derived class.

#include <iostream>
using namespace std;

class Base {
   public:
      void display(){
      cout << "Base class function called" << endl;
   }
};

class Derived : public Base {
   public:
   void display(){
     cout << "Derived class function called" << endl;
   }

   void showBoth(){
      Base::display(); // Base class function using SRO
      display();       // derived class function
   }
};

int main() {
   Derived d;
   d.showBoth();
   return 0;
}

The output of the above code is as follows −

Base class function called
Derived class function called

Example: Multiple Inheritance

In multiple inheritance, there can be more than one function or variable with same name in different base classes that can cause ambiguity. This can be solved using scope resolution operator. Here is an example where display() function is called using (::) to solve this ambiguity.

#include <iostream>
using namespace std;

class ClassA {
   public:
   void display(){
      cout << "Display function from ClassA" << endl;
   }
};

class ClassB {
   public:
   void display(){
      cout << "Display function from ClassB" << endl;
   }
};

// Multiple Inheritance
class ClassC : public ClassA, public ClassB {
   public:
   void showAll() {
      ClassA::display(); // Calling ClassA's display using SRO
      ClassB::display(); // Calling ClassB's display using SRO
   }
};

int main(){
   ClassC obj;
   obj.showAll();
   return 0;
}

The output of the above code is as follows −

Display function from ClassA
Display function from ClassB

Conclusion

In this chapter, we discussed about the scope resolution operator and its various applications. Like other operators, scope resolution operator can not be overloaded. The main purpose of (::) operator is to solve the ambiguity if the variable or the functions are having same name. At compile time, scope resolution operator tells compiler about the variables and functions where they are used in the code. This solves the ambiguity caused by similar names.

Advertisements