CppBuzz.com
  
Home C C++ Java Python Perl PHP Spring SQL Javascript Linux Forum

You are here : Home » How to access private members of a C++ class from outside

How to access private members of a C++ class from outside

// filename :- Hack.h

class Hack
{
 private: int a;

 protected: int b;

 public: int c;

}; //end of Hack.h


// filename :- main.cpp

#include

#define private public //This is the hack to access priviate variables of any class
#define protected public // This is the hack to access protected variables of any class

#include "Hack.h"
using namespace std;

int main(int argc, char** argv)
{
 Hack obj;

 obj.a= 10; // We can successully access a which is private actully
 obj.b= 20; // We can successfully access b which is protected actually

 obj.c = 30;

 cout<<obj.a;
 cout<<obj.b;
 cout<<obj.c;

 return 0;

} //end of main.cpp

Other links :

MCQ on C Programming  |  MCQ on C++ Programming  |  138+C++ F2F Interview Questions |  Basic Computer Questions |  Solved C programs  |  Solved C++ programs