This section will discuss the scope resolution operator and its various uses in the C++ programming language. The scope resolution operator is used to reference the global variable or member function that is out of scope. Therefore, we use the scope resolution operator to access the hidden variable or function of a program. The operator is represented as the double colon (::) symbol.
For example, when the global and local variable or function has the same name in a program, and when we call the variable, by default it only accesses the inner or local variable without calling the global variable. In this way, it hides the global variable or function. To overcome this situation, we use the scope resolution operator to fetch a program's hidden variable or function.
Uses of the scope resolution Operator
- It is used to access the hidden variables or member functions of a program.
- It defines the member function outside of the class using the scope resolution.
- It is used to access the static variable and static function of a class.
- The scope resolution operator is used to override function in the Inheritance.
Program to access the hidden value using the scope resolution (::) operator
Program1.cpp
#include <iostream>
using namespace std;
// declare global variable
int num = 50;
int main ()
{
// declare local variable
int num = 100;
// print the value of the variables
cout << " The value of the local variable num: " << num;
// use scope resolution operator (::) to access the global variable
cout << "\n The value of the global variable num: " << ::num;
return 0;
}
Output
The value of the local variable num: 100
The value of the global variable num: 50
Program to define the member function outside of the class using the scope resolution (::) operator
Program2.cpp
#include <iostream>
using namespace std;
class Operate
{
public:
// declaration of the member function
void fun();
};
// define the member function outside the class.
void Operate::fun() /* return_type Class_Name::function_name */
{
cout << " It is the member function of the class. ";
}
int main ()
{
// create an object of the class Operate
Operate op;
op.fun();
return 0;
}
Output
It is the member function of the class.
Program to demonstrate the standard namespace using the scope resolution (::) operator
Program3.cpp
#include <iostream>
int main ()
{
int num = 0;
// use scope resolution operator with std namespace
std :: cout << " Enter the value of num: ";
std::cin >> num;
std:: cout << " The value of num is: " << num;
}
Output
Enter the value of num: 50
The value of num is: 50
Program to access the static variable using the scope resolution (::) operator
Program4.cpp
#include <iostream>
using namespace std;
class Parent
{
static int n1;
public:
static int n2;
// The class member can be accessed using the scope resolution operator.
void fun1 ( int n1)
{
// n1 is accessed by the scope resolution operator (:: )
cout << " The value of the static integer n1: " << Parent::n1;
cout << " \n The value of the local variable n1: " << n1;
}
};
// define a static member explicitly using :: operator
int Parent::n1 = 5; // declare the value of the variable n1
int Parent::n2 = 10;
int main ()
{
Parent b;
int n1 = 15;
b.fun1 (n1);
cout << " \n The value of the Base::n2 = " << Parent::n2;
return 0;
}
Output
The value of the static integer n1: 5
The value of the local variable n1: 15
The value of the Base::n2 = 10
Program to access the static member function using the scope resolution (::) operator
Program5.cpp
#include <iostream>
using namespace std;
class ABC
{
public:
// declare static member function
static int fun()
{
cout << " \n Use scope resolution operator to access the static member. ";
}
};
int main ()
{
// class_name :: function name
ABC :: fun ();
return 0;
}
Output
Use scope resolution operator to access the static member.
Program to override the member function using the scope resolution (::) operator
Program5.cpp
#include <iostream>
using namespace std;
class ABC
{
// declare access specifier
public:
void test ()
{
cout << " \n It is the test() function of the ABC class. ";
}
};
// derive the functionality or member function of the base class
class child : public ABC
{
public:
void test()
{
ABC::test();
cout << " \n It is the test() function of the child class. ";
}
};
int main ()
{
// create object of the derived class
child ch;
ch.test();
return 0;
}
Output
It is the test() function of the ABC class.
It is the test() function of the child class.