0% found this document useful (0 votes)
187 views

Scope Resolution Oprator

The scope resolution operator (::) is used to access global variables or call functions defined outside a class when a local variable or function of the same name exists. It allows specifying that a variable or function belongs to the global namespace rather than the local scope. For example, ::c accesses the global variable c rather than a local c, and programming::output() calls the output() function defined for the programming class rather than a local function.

Uploaded by

Brahmanand Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views

Scope Resolution Oprator

The scope resolution operator (::) is used to access global variables or call functions defined outside a class when a local variable or function of the same name exists. It allows specifying that a variable or function belongs to the global namespace rather than the local scope. For example, ::c accesses the global variable c rather than a local c, and programming::output() calls the output() function defined for the programming class rather than a local function.

Uploaded by

Brahmanand Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Scope resolution operator in c++

Scope resolution operator(::) is used to define a function outside a class or when we want to use
a global variable but also has a local variable with same name.

C++ programming code


#include <iostream>
using namespace std;
char c = 'a';

// global variable

int main() {
char c = 'b';

//local variable

cout << "Local c: " << c << "\n";


cout << "Global c: " << ::c << "\n";
}

//using scope resolution operator

return 0;

Scope resolution operator in class


#include <iostream>
using namespace std;
class programming {
public:
void output(); //function declaration
};
// function definition outside the class
void programming::output() {
cout << "Function defined outside the class.\n";
}
int main() {
programming x;
x.output();
return 0;
}

You might also like