Working of scope resolution operator in C++ (::)

Scope resolution operator in C++

Working of scope resolution operator in C++ (::)



Working of scope resolution operator in C++ (::)

:: (Double colon) represents scope resolution operator in C+.  It can be used in multiple ways in C++.

i. To refer global variables

Scope resolution operator in C++can refer global variable in a C++ program.

#include<iostream>
using namespace std;
int a=10;         //Global Variable a
int main()
{
int a=5;         //Local Variable a
cout<<”\na=”<<a; //Value of local variable num is 5
cout<<”\nGlobal variable a=”<<::a; // Value of global variable a is 10.
return 0;
}

Output

a=5
Global variable a=10

 ii. To distinguish between members and non members of a class.

Scope resolution operator in C++ can distinguish between normal variables/functions and data members/member functions  of a class .

Example

class A
{
public:
  int n;
  void disp(); 
};   

Data member n of class A can be referred  as A::n and  member function disp() of class A can be referred as A::disp().
 

iii. To define member functions outside the class.

Scope resolution operator in C++ can be used to define member function of a class outside the class. We need to be declare a function inside the class before defining it outside the class.

Example

#include<iostream>
using namespace std;
class demo
{
int n;
public:
  void show();
};
void data::show()
{
  n=10
  cout<<”\nn=”<<n;
}
int main()
{
 demo ob;
 ob.show();
 return 0;
}

Output

n=15

 iv. To declare object of a nested class

Scope resolution operator in C++ can be used to declare object of a nested class. We should write the name of container class followed by scope resolution operator which is further followed by name of nested class and then name of object that is to be declared.

Example

#include<iostream>
using namespace std;
class outer
{
int n;
public:
void outerreadshow()
 {
 cout<<”\nType value of n=”;
 cin>>n;
 cout<<”\nn=”<<n;
 }         
 class inner
  {
   int m;
   public:
     void innerreadshow()
     {
     cout<<”\nType value of m=”;
     cin>>m;
     cout<<”\nm=”<<m;
     }
  };
};
int main()
{
outer::inner obj;  //Object of nested class inner declared.
obj.innerreadshow();
return 0;
}

Output

Type value of m=5
m=5

v. To declare and define static data members of class

Scope resolution operator in C++is used  to initialize a static data member outside the class.

We can also use this operator to call static member function outside the class.

Example

class data
{
static int n; //Declaration of static data member n inside class
static void disp()  //Static member function declared 
 {
  cout<<n;
 }
};
int data::n=0; //Initializing static data member n outside the class.
int main()
{
data::disp() //Static member function disp() is called outside the class as data::disp().
}

 



Spread the love
Lesson tags: < operator in C++, scope resolution operator in C++, Scope resolution operator to declare and define static data members of class, To refer global variables in C++
Back to: C++ Programming Notes
Spread the love