Difficulty: Easy
Correct Answer: private
Explanation:
Introduction / Context:
Access control is fundamental to encapsulation. In C++, the default accessibility of members differs between class
and struct
. Understanding the default helps you avoid unintended exposure of implementation details and enforces safer APIs by default.
Given Data / Assumptions:
class
block.
Concept / Approach:
Inside a class
, the default access for members is private. Inside a struct
, the default is public. This asymmetry is deliberate: class
encourages encapsulation by hiding internals unless explicitly exposed, while struct
is often used for simple aggregates where public data is acceptable. You can always override defaults by writing explicit public:
, protected:
, or private:
labels.
Step-by-Step Solution:
Verification / Alternative check:
Try accessing a member from outside the class without an explicit public:
section; the compiler emits an access error, confirming the default is private for classes.
Why Other Options Are Wrong:
public/protected: not the default in classes.
friend: not an access level for members; it is a relationship granting access to another function/class.
Common Pitfalls:
Final Answer:
private
Discussion & Comments