Search This Blog

Thursday 28 January 2016

Write a program to demonstrate the use of Scope Resolution Operator:: with variable name.

Without Class Example:

#include <iostream.h>

char n = 'k';   // A global variable

int main() {

   char n = 'g';   // A local variable
   cout  << ::n << endl;  // Print the global variable: k
   cout  << n   << endl;  // Print the local variable: g
}




With Class Example

#include <iostream.h>

class gajjar {
public:
  void output();
};

void gajjar::output() {
  cout << "Outside the class.\n";
}

int main() {
  gajjar x;
  x.output();

  return 0;

}

No comments:

Post a Comment