Scope Resolution Operator in C++

Google 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 Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

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

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483