Machine-translated by Google. Questions are authored in English.
Top

Discussion

In C++, which of the following is true for private members of a class ?

  • A.They can be accessed directly by any other class
  • B.They are accessible only within the same class
  • C.They are accessible in the derived classes
  • D.They are accessible through this pointer

Answer: B

Private members are accessible only within the same class.

In C++, private members can be used only by the class's own member functions (and by its friends). This is the basis of encapsulation.

They cannot be accessed by other classes, and derived classes cannot access them directly either; that is what protected members allow. The this pointer is used inside member functions, so it does not open private members to outside code.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.