Friend Function in C++

Image
Friend Function in C++ Welcome to C++ Programming. In this article, we will talk about friend Function and friend Class trying to understand what do they mean and why are they useful in C++ design process. These will be the module outline we will take examples of Matrix Vector Multiplication and Linked List, and finally will end with somenotes. As you know as usual the outline will be available on the left of your slide So, let us first introduce the basic notion of friend function. On the left you have the view of ordinary function. So the situation is like this, that I have a class myclass which as some private data, it has a constructor, and it has; I am sorry if just ignore this line. I have this function written outside of this class which is trying to do something with the class. What is it doing? It takes an object of the class by reference parameter call by reference, and then it tries to print the data element component of that objective. Now, what do we know, this is pr...

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++

Programming language in C++

How To Use Class and Object In C++