Other Functionality

                          Other Functionality


Scope Resolution Operator

Scope Resolution Operator, denoted by the symbol :: (double colon) has two uses:
i) For defining member functions of class outside the class
ii) To access the global version of a variable

For defining member functions of class outside the class

We can define member function of a class outside the class definition using scope resolution operator (denoted as double colon sign ::)

Example of defining member function outside class

class abc
{
public:
void fun(); //member function of class abc
}; // class definition closed

void abc::fun() //member function fun of class abc is defined outside class definition
{
cout<<"hello";
}

void main()
{
abc ob;
ob.fun(); //member function fun called
}

To access the global version of a variable


Second use of scope resolution operator is to access the global version of a variable (this cannot be done in C). For example ::count means the global version of the variable count (and not the local variable count declared in that block)

Example of :: to access global version of a variable

#include<iostream.h>
#include<conio.h>
int m=10; //global m
void main()
{
clrscr();
int m=20; //m redeclared, local to main
{
int k=m;
int m=30; //m declared again,this time, local to inner block
cout<<"We are in inner block\n";
cout<<"k = "<<k<<"\n";
cout<<"m = "<<m<<"\n";
cout<<"::m = "<<::m<<"\n";
}
cout<<"\nWe are in outer block\n";
cout<<"m = "<<m<<"\n";
cout<<"::m = "<<::m<<"\n";
getch();
}

Output:
We are in inner block
k = 20
m = 30
::m = 10


Comments