C++ scope resolution operator
इस operator का use global variable के लिए किया जाता है जहाँ किसी program में global और local variable दोनों का नाम समान होते हैं ऐसे में global variable को access करने के लिए scope resolution operator (::) का use किया जाता है।
आप इसे नीचे दिए गए program से समझ सकते हैं-
scope resolution operator example in C++
#include<conio.h>
#include<iostream.h>
int num=3; // global varaible
void main()
{
clrscr();
int num = 6; //local variable
cout<<"local num : "<<num<<endl; // for local variable
cout<<"global ::num: "<<::num; // global variable
getch();
}
OUTPUT
local num : 6
global ::num: 3
इसका प्रयोग class और structure के members की outline definition में भी किया जाता है इसके बारे में आगे इसी tutorial में बताया गया है।
#include<iostream.h>
#include<conio.h>
class student
{
// private by default
int roll_no;
char name[20];
public: // public member
void get_record(void);
void put_record(void);
}; class closed
//input record from the user
void student::get_record()
{
cout<<"Enter roll no: ";
cin>>roll_no; cout<<"Enter Name : ";
gets(name);
}
//display record
void student::put_record()
{
cout<<"\nRoll no: "<<roll_no;
cout<<"\nName : "<<name;
}
// main program start
void main()
{
clrscr();
student x;
x.get_record();
x.put_record();
getch();
}
OUTPUT
Enter roll no: 11
Enter Name : Rahul sherma
Roll no: 11
Name : Rahul sherma