How to access private data members of a class in c++


Access Private data members of a class

                        Many people ask the same question can we access the private data member of a class the answer is yes you can access the private data member of a class and also you change the value of that data now you will say how: just remember that c++ supports pointers which is very powerful concept and it is always diffcult to manage it in large project.


                                We will use the pointer to access the private data member simple make a class x and having two private data member of type int and one public method void set() just set the variable to anything.

                    
                                Now create the main() method and make object of  x  obj then make pointer variable int *p after that we will do typecasting like that p=<int*>&obj now the last step is print the *p and you will see the value of both private data members.


                                In the end i would like to say that you have access the private data member in main method now you can change their value also.If you have any problem just do comment.



How to access private data members of a class in c++








Comments

Popular posts from this blog

Friend Function in C++

Software engineer

Data Member and Static Keyword in C++