- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Input Output Operations
- C++ Basic Input/Output
- C++ Cin
- C++ Cout
- C++ Manipulators
- Type System & Data Representation
- C++ Modifier Types
- C++ Storage Classes
- C++ Constexpr Specifier
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Scope Resolution Operator
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ Jump Statements
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Return Values
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Array Decay
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Destructors
- C++ Virtual Destructor
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ Override Specifiers
- C++ Final Specifiers
- C++ Design Patterns
- C++ Creational Design Patterns
- C++ Singleton Design Pattern
- C++ Factory Method Design Pattern
- C++ Abstract Factory Pattern
- C++ Prototype Design Pattern
- C++ Structural Design Patterns
- C++ Facade Design Pattern
- C++ Iterator Design Pattern
- C++ Mediator Design Pattern
- C++ Memento Design Pattern
- C++ Observer Design Pattern
- C++ State Design Pattern
- C++ Strategy Design Pattern
- C++ Template Method Design Pattern
- C++ Visitor Design Pattern
- C++ Behavioural Design Pattern
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Move Semantics
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ nullptr
- C++ unordered_multiset
- C++ Chain of Responsibility
- C++ Structural Design Patterns
- C++ Adapter Pattern
- C++ Bridge Pattern
- C++ Composite Pattern
- C++ Decorator Pattern
- C++ Command Pattern
- C++ Proxy Pattern
- C++ Useful Resources
- C++ Questions and Answers
- C++ Quick Guide
- C++ Cheatsheet
- C++ STL Tutorial
- C++ Standard Library
- C++ Useful Resources
- C++ Discussion
- C++ Online Compiler
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
- Defining a Function Outside Class
- Accessing a Global Variable
- Accessing static Member Variables
- Iterator Declaration
- Calling Parent Class Function in Overriding
- Demonstrating Inheritance
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.