Scope Resolution Operator In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Scope Resolution Operator In C++

Scope Resolution Operator In C++

BLUF: Mastering Scope Resolution Operator In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Scope Resolution Operator In C++

C++ is renowned for its efficiency. Learn how Scope Resolution Operator In C++ enables low-level control and high-performance computing in the tutorial below.

This part will cover the scope resolution operator and its different applications in the C++ programming language. The scope resolution operator is employed to access a global variable or member function that is not within the current scope. Hence, we utilize the scope resolution operator to reach the concealed variable or function within a program. This operator is denoted by the double colon (::) symbol.

For instance, in a scenario where a program contains both global and local variables or functions with identical names, the default behavior is for the program to access the local variable or function when called, effectively hiding the global one. To address this issue, programmers can utilize the scope resolution operator to retrieve the concealed global variable or function within the program.

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

Example

#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

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

Example

#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

Output

It is the member function of the class.

Program to demonstrate the standard namespace using the scope resolution (::) operator

Program3.cpp

Example

#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

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

Example

#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

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

Example

#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

Output

Use scope resolution operator to access the static member.

Program to override the member function using the scope resolution (::) operator

Program5.cpp

Example

#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

Output

It is the test() function of the ABC class.
 It is the test() function of the child class.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience