
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Scope Resolution Operator
C++ scope resolution operator is used to access the variables, functions, and classes which are defined outside the current scope.
What is Scope Resolution Operator (SRO) in C++?
In C++, scope resolution operator is used to define a function outside the class and used to access the static variables of class. It access the identifiers like class and namespace. It is denoted by double colon (::)
Note: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable.
Syntax
The basic syntax of scope resolution operator as follows:
scope::identifier
Here,
- scope: It can be a class name, namespace, or, global.
- identifier: The identifier can be a variable, function, type, or, constant.
To learn how to use the scope resolution operator, follow the below approaches:
- Scope Resolution Operator with the std Namespace
- Scope Resolution Operator for Demonstrating Inheritance
- Accessing Global Variables Using the Scope Resolution Operator
- Accessing Class Static Members Using the Scope Resolution Operator
Scope Resolution Operator with the std Namespace
In C++, the standard function and objects are defined inside the std namespace. You can access cout from std namespace using scope resolution operator say std::.
Example 1
In this example, we show the basic usage of scope resolution operator using std namespace.
#include <iostream> int main() { // Accessing cout from std namespace using scope // resolution operator std::cout << "Tutorialspoint"; return 0; }
The above program produces the following result:
Tutorialspoint
Example 2
Below the program declared TP namespace to the global scope, that means it can be accessed anywhere in the program without restriction, using the TP:: prefix to refer to its members and display the output.
#include <iostream> using namespace std; // A sample namespace with a variable namespace TP { int val = 20; }; int main() { // Accessing val from namespace N cout << TP::val; return 0; }
The above program produces the following result:
20
Scope Resolution Operator for Demonstrating Inheritance
The inheritance is the concept of Object Oriented Programming that allows one class to inherit properties, methods, and behavior from another class. The inherited class is known as child class or derieved class. Here, you can use :: operator to access base class members from a derived class.
Example
In this example, we demonstrates the use of scope resolution operator (::) with the help of inheritance.
#include <iostream> using namespace std; class Base { public: void show() { cout << "Base class show function" << endl; } }; class Derived : public Base { public: void show() { cout << "Derived class show function" << endl; } void display() { // Call the base class show function using :: Base::show(); } }; int main() { Derived d; // Call derieved class show function d.show(); // Calls Base class show function using :: d.display(); return 0; }
The above program produces the following result:
Derived class show function Base class show function
Accessing Global Variables Using the Scope Resolution Operator
Here, we define global variable x = 3 and the local variable x = 10 inside the main() function, where local variable hides the global variable. By using scope resolution operator (::), the program access and display the global value of x.
Example
In this example, we demonstrates how local variable hides the global variable.
#include <iostream> using namespace std; // Global x int x = 3; int main() { // Local x int x = 10; // display the global x cout << ::x; return 0; }
The above program produces the following result:
3
Accessing Class Static Members Using the Scope Resolution Operator
In C++, static data members are part of the class, and the variable can be accessed by all instances of the class. Here, we have main() function in which static member is accessed using A::uniqueCounter without creating any object and its value displayed using cout.
Example
In this example, we show the usage of static data member using scope resolution operator.
#include<iostream> using namespace std; class A { public: static int uniqueNum; }; // Defining the static member outside the class int A::uniqueNum = 1; int main() { // Accessing static data member cout << A::uniqueNum; return 0; }
The above program produces the following result:
1