Sunday 26 July 2015

  • Sunday, July 26, 2015

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;
}
Related Posts Plugin for WordPress, Blogger...

Vacancy

RBI has announced the recruitment for 134 GRADE B OFFICERS.
 The examination will be in two phases.
 The detailed advertisement will be published on 5th October.

Popular Posts