Symbol of Scope Resolution Operator in C++ is : :
when using Scope Resolution Operator in C++?
- To Define function outside of the class
- To Define the value of the static variable
- Access Global variable

1) To Define function outside of the class
When You create a Class and write a function definition, and you want to declare a function outside of the class so you must use a scope resolution operator.
Syntax
Return_type Class_name : : function_name (parameter ) { // code of function }
Example
#include<iostream> using namespace std; class test{ public: int a=20, b=25, c; void sum(void); }; void test :: sum(){ c = a + b; cout<<"sum of a + b = "<<c; } int main(){ test t1; t1.sum(); return 0; }
Output
sum of a + b = 45
2) To Define the value of a static variable
When You define a Static variable in the class, It has a value is zero. if You want to define some value to a static variable, you can not give a value without using the scope resolution operator.
Also Read This :- Write a Program to Sum of Two Numbers in c++
Syntax
Variable_Type Class_name : : variable_name = value
Example
#include<iostream> using namespace std; class test{ public: static int a; }; int test :: a = 10; int main(){ test t1; cout<<"current value of a = "<<t1.a; return 0; }
output
current value of a = 10
3) Access the Global variable
If your program has the same name as the global variable and a local variable, so global variable you can not use them directly. and you want to use a global variable, then you must require the use of scope resolution operators.
Syntax
: : variable_name.
Example
#include<iostream> using namespace std; int x = 10; int main(){ int x = 30; cout<<"Global value of a = "<<::x<<endl; cout<<"local value of a = "<<x; return 0; }
output
Global value of a = 10 local value of a = 30