Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


How can a function outside a class access private members of the class?

  1. By being declared a friend within the class

  2. By inheriting the class

  3. Using public member functions

  4. It cannot access private members

The correct answer is: By being declared a friend within the class

Functions outside of a class cannot directly access private members of that class. However, by being declared a friend within the class, the function is granted permission to access private members. Inheritance (B) can provide access to protected members, but not private ones. Using public member functions (C) also does not give access to private members. Option D is also incorrect because with the appropriate declaration, functions can access private members of a class.