Which of the following can access private data members or member functions of a class ?
A. Any function in the program.
B. All global functions in the program.
C. Any member function of that class.
D. Only public member functions of that class.
Answer : C. Any member function of that class.
Explanation :
- Accessing a data member depends only on the access control of that data member. If its public, then the data member can be easily accessed using the direct member access (.) operator with the object of that class.
- If, the data member is defined as private or protected, then we cannot access the data variables directly. Then we will have to create special public member functions to access, use or initialize the private and protected data members.
- These member functions are also called Accessors and Mutator methods or getter and setter functions.