C++ program to demonstrate use of Scope Resolution Operator

Learn about the Scope Resolution Operator (SRO) in C++ with examples.
[Last updated : March 01, 2023]

Scope Resolution Operator in C++

Scope Resolution Operator, generally known as SRO (::) is used to access the hidden names so that you can still use them. It's a unary operator and is able to access the hidden members of a namespace scope or global scope by an explicit declaration of the same name in a block or class.

In this example you will learn how to use SRO (Scope Resolution Operator) in c++ programming language, this is the operator using this operator we can access global values of the program.

Scope Resolution Operator Example in C++

/*C++ program to demonstrate use of Scope Resolution Operator,
Use global variable in local scope*/
 
#include  <iostream>
using namespace std;
int a = 20;
 
int main()
{
    int a = 10;
 
    cout << "Value of local  a: " << a << endl;
 
    //use of SRO (::) to access global variable.
    cout << "Value of global a: " << ::a << endl;
 
    return 0;
}

Output

    Value of local  a: 10
    Value of global a: 20




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.