C++ - Access global variables using Scope Resolution Operator (SRO).


IncludeHelp 03 July 2016

SRO (Scope Resolution Operator - :: ) is an operator which is used to access global variable and define class functions outside of the class, in this code snippet we will access global variables inside the main function using SRO (::).

C++ Code Snippet - Access Global Variables using Scope Resolution Operator (SRO)

/*C++ - Access global variables using 
Scope Resolution Operator (SRO).*/

#include <iostream>
using namespace std;

//global variables
int a=10, b=20;

int main(){
	
	int a=100, b=200;
	
	cout<<"Local variables  a: "<<a<<", b: "<<b<<endl;
	cout<<"Global variables a: "<<::a<<", b: "<<::b<<endl;

}
    Local variables  a: 100, b: 200
    Global variables a: 10, b: 20





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.