Advertisement
#include<iostream>
using namespace std;
int x =40; //Global version of variable, x, because it is declared and defined outside any function
int main()
{
{
int x = 10; //Local version of the variable, x
}
{
int x = 20; //Local version of the variable, x
}
{
int x = 30; //Local version of the variable, x
}
}
//C++ Example of Scope Resolution Operator
#include<iostream>
using namespace std;
int a = 10; //Global version of variable, a
int main()
{
int a = 20; //First local version of variable, a
{
int a =30; //Second local version of variable, a
cout<<"The value of second local version of a : " << a <<"\n";
cout<<"The value of global version of a : " << ::a << "\n"; ;
}
cout<< "The value of first local version of a : " << a << "\n";
cout<< "The value of global version of a : "<< ::a;
}
The value of second local version of a : 30
The value of global version of a : 10
The value of first local version of a : 20
The value of global version of a : 10
Advertisement
#C++ Example of Scope Resolution Operator
#include<iostream>
using namespace std;
class X
{
private :
int x1;
public :
int get_x1();
void set_x1();
};
//Using Scope Resolution Operator to define get_x1() function of X class
int X :: get_x1()
{
return x1;
}
//Using Scope Resolution Operator to define set_x1() function of X class
void X :: set_x1(int x)
{
x1 = x;
}
int main()
{
X ob;
ob.set_x1(10);
cout << "The value of x is : " << ob.get_x1(10);
}
The value of x is : 10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement