Scope Resolution Operator in C++

Advertisements

Prev Tutorial Next Tutorial

Scope Resolution Operator in C++

The scope resolution operator is denoted by :: in C++. Scope resolution operator (::) in C++ programming language is used to define a function outside a class or when we want to use a global variable but also has a local variable with the same name.

The scope resolution operator (::) basically does two things

  • Access the global variable when we have a local variable with same name
  • Define a function outside the class

Syntax of Scope Resolution Operator

	
:: global variable name

Resolution operator is placed between the front of the variable name then the global variable is affected.If no resolution operator is placed between the local variable is affected.

Using Scope Resolution Operator

	
#include<iostream.h>
#include<conio.h>

int n=12; //global variable
int main()
{
int n=13; //local variable
cout<<::n<<endl; //print global variable:12
cout<<n<<endl; //print the local variable:13
getch();
}

Output

	
12
13

If the resolution operator is placed between the class name and the data member belonging the class then data name belonging to the particularly class is affected.

If it is place front of the variable name then the global variable is affected. It is no resolution operator is placed then the local variable is affected.

Without Scope Resolution Operator

	
#include<iostream.h>
#include<conio.h>
 
int a = 100; // global variable a = 100
 
int main() {
 
  int a = 200;   // local variable a = 200
 
  cout << a << "\n"; // print a      
  
getch();
}

Since, you have defined a = 200 locally (within the int main() function) , the output will be 200 not 100.

Output

	
200

Scope Resolution Operator in C++

	
#include<iostream.h>
#include<conio.h>
 
int a = 100;     // global variable
 
void main() 
{
  int a = 200;   //local variable
  clrscr();
  cout<<"Local  variable: " << a << "\n";      
  cout<<"Global variable: " << ::a << "\n";  //using scope resolution operator
 
getch();
}

Output

	
Local  variable: 200
Global variable: 100

Defining a Function Outside the Class

Scope Resolution Operator in class in C++

	
#include<iostream.h>
#include<conio.h>
 
class scope_resolution 
{
public:
  void output();  //function declaration
};
 
// function definition outside the class
 
void scope_resolution::output() 
{
  cout << "Function defined outside the class.\n";
}
 
void main() 
{
  clrscr();
  scope_resolution x;
  x.output();
 
 getch();
}

Output

	
Function defined outside the class

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements