cppbuzz logo

Home  |  About Us  |  Contact
View Questions Upload Training Misc..

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


#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;


 return 0;

} //end of main.cpp

Other links :

MCQ on C Programming  |  MCQ on C++ Programming