Scope Resolution Operator
Scope Resolution Operator
Scope Resolution Operator is used in C++. Scope Resolution Operator is used for different purposes for different reasons. There are many purposes like avoiding the name conflict, Access the global variable, and other different use.
scope resolution operator is denoted by _::__.
Now we will see one by one the different purposes of the scope resolution operator in oop.
- scope resolution operator to access global variables when the local and global variables have the same name.
#include<iostream>
using namespace std;
int x; // Global x
int main()
{
int x = 10; // Local x
cout << "The value of a global variable of the x is " << ::x;
cout << "\nThe value of a local variable of the x is " << x;
return 0;
}
Output :
The value of a global variable of the x is 0.
The value of a local variable of the x is 10.
- Scope Resolution Operator and Functions have some relation in oops. This operator is used to define the function outside the class.
#include<iostream>
using namespace std;
class A
{
public:
void fun(); //Declaration Of The Function.
};
void A::fun() // Definition function outside class using ::
{
cout << "fun() called using Scope resolution Operator.";
}
int main()
{
A a;
a.fun();
return 0;
}
Output :
fun() called using Scope resolution Operator.
- Scope Resolution Operator in C++ used to access the static variable in our program.
#include<iostream>
using namespace std;
class Test
{
static int x;
public:
static int y;
// Local parameter 'a' hides class member
// 'a', but we can access it using ::
void func(int x)
{
// We can access class's static variable
// even if there is a local variable
cout << "The Value of static x is " << Test::x;
cout << "\n The Value of local x is " << x;
}
};
int Test::x = 1; // In C++, static members must be explicitly defined like this .
int Test::y = 2;
int main()
{
Test obj;
int x = 3 ;
obj.func(x);
cout << "\nTest::y = " << Test::y;
return 0;
}
Output :
The Value of static x is 1
The Value of local x is 3
Test::y = 2
- When we used the multiple inheritances if the same name variables exist, We can use the Scope Resolution Operator to distinguish between them.
#include<iostream>
using namespace std;
class A
{
protected:
int x;
public:
A() { x = 10; }
};
class B
{
protected:
int x;
public:
B() { x = 20; }
};
class C: public A, public B //Multiple Inheritances.
{
public:
void fun()
{
cout << "A's x is " << A::x;
cout << "\nB's x is " << B::x;
}
};
int main()
{
C c;
c.fun();
return 0;
}
Output :
A's x is 10
B's X is 20.
- Application of scope resolution operator in C++ of best is namespaces. If the class has the same exist inside the namespaces so we can access the class using the Scope Resolution Operator without any conflict.
#include<iostream>
int main(){
std::cout << "Hello" << std::endl;
}
Output :
Cout and endl belong to the std namespace.
So, there are lots of the Application of scope resolution operator in C++. Above all points is the use of the scope resolution operator. So you can also try the use of the scope resolution operator yourself doing the program of all the above topics. Now you can define yourself by the scope resolution operator is used with different purposes. In your mind one question also that scope resolution operator can be overloaded.
Note :
Scope Resolution Operator can't be overloaded.
Comments
Post a Comment
if you have any doubts, let me know