In C++, the scope resolution operator is ::. It is used for the following purposes.
1) To access a global variable when there is a local variable with same name:
CPP
#include<iostream>
using namespace std;
int x;
int main()
{
int x = 10;
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
|
Output
Value of global x is 0
Value of local x is 10
2) To define a function outside a class.
CPP
#include <iostream>
using namespace std;
class A {
public:
void fun();
};
void A::fun() { cout << "fun() called"; }
int main()
{
A a;
a.fun();
return 0;
}
|
3) To access a class’s static variables.
CPP
#include<iostream>
using namespace std;
class Test
{
static int x;
public:
static int y;
void func(int x)
{
cout << "Value of static x is " << Test::x;
cout << "\nValue of local x is " << x;
}
};
int Test::x = 1;
int Test::y = 2;
int main()
{
Test obj;
int x = 3 ;
obj.func(x);
cout << "\nTest::y = " << Test::y;
return 0;
}
|
Output
Value of static x is 1
Value of local x is 3
Test::y = 2
4) In case of multiple Inheritance: If the same variable name exists in two ancestor classes, we can use scope resolution operator to distinguish.
CPP
#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
{
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
5) For namespace If a class having the same name exists inside two namespaces we can use the namespace name with the scope resolution operator to refer that class without any conflicts
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define nline "\n"
string name1 = "GFG";
string favlang = "python";
string companyName = "GFG_2.0";
class Developer {
public:
string name = "krishna";
string favLang = "c++";
string company = "GFG";
Developer(string favlang, string company)
: favLang(favlang)
, company(companyName)
{
}
};
int main()
{
Developer obj = Developer("python", "GFG");
cout << "favourite Language : " << obj.favLang << endl;
cout << "company Name : " << obj.company << nline;
}
|
Output
favourite Language : python
company Name : GFG_2.0
6) Refer to a class inside another class: If a class exists inside another class we can use the nesting class to refer the nested class using the scope resolution operator
CPP
#include <iostream>
using namespace std;
class outside {
public:
int x;
class inside {
public:
int x;
static int y;
int foo();
};
};
int outside::inside::y = 5;
int main()
{
outside A;
outside::inside B;
}
|
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
08 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...