Define SRO in C++

Define SRO in C++

Scope resolution operator (::)
In C++ programming language, it 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 the same name.

Syntax with function:
retrun_Type class_name :: function_name()
{
}

Syntax to access global variable:
::global_varible

Scope resolution operator with global variable:

program:

#include< iostream>
using namespace std;
//global variable
char c = 'a';
int main(){
//local variable
char c = 'b';
cout << "Local variable: " << c << "n";
//using scope resolution operator
cout << "Global variable: " << ::c << "n";
return 0;
}


output-

Local variable: b
Global variable: a


Scope resolution operator with function:

program:

#include< iostream>

using namespace std;
class Myclass
{
public:
void function();
};
void Myclass::function()
{
cout<<"This is my function outside class";
}
int main()
{
Myclass obj;
obj.function();
return 0;
}


output-

This is my function outside class




Read More →
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!