:: is called the scope resolution operator in C++ programs. Any variable declared above void main () function ( not within any function bode, just after the include statements ) is known as global variable. Global variables are accessed using scope resolution operator. Any variable declared within void main () body is known as local variable. local and global variables may have the same name.
Here is a C++ program to demonstrate the use of scope resolution operator
#include< iostream.h >
#include< conio.h >
int i;
void main()
{
clrscr();
int i;
cout<< endl<< "Enter value for the local variable:";
cin>>i;
cout<<endl<<"Enter value for the global variable:";
cin>>::i;
cout<< endl<< "Local="<< i;
cout<< endl<< "Global="<< ::i;
getch();
}
No comments:
Post a Comment