
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
What is the meaning of prepended double colon “::” in C++?
The prepended double colon (::) is also known as the scope resolution operator. The scope resolution operator serves various purposes in C++. In this article, we will discuss the applications of the scope resolution operator in C++ with example codes of each application.
Uses of Scope Resolution Operator in C++
The scope resolution operator serves various purposes in C++. We have listed below 5 applications of the scope resolution operator:
- The scope resolution operator is used to define a function outside the class.
- It is used to access a global variable when there is already a local variable present with the same name.
- It is used to define a static variable outside the class and is also used to access the static variable using the class name.
- It is used in referring to a namespace member, such as: std::cout, std::cin, and std::endl.
- If a function is present in both classes, i.e., parent class and child class, and you want to call the function from the parent class, then the scope resolution operator is used.
Defining a Function Outside Class
To define a function outside the class, we use the scope resolution operator. Here is an example:
Example
In this example, we have initialized 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 a 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.
Example
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 are defined outside the class using the scope resolution operator, and since they are object-independent, they can be accessed using a class name with the help of the scope resolution operator.
Example
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
Referring a namespace Member
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 '::'.
Example
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::
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 already present in the child class using the child class object.
Example
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