C++ classes: what is the default access specifier used inside a class definition (if none is written)?

Difficulty: Easy

Correct Answer: Private

Explanation:


Introduction / Context:
C++ distinguishes between classes and structs not by features but by defaults. Both support the same mechanisms: member functions, constructors, inheritance, templates, and so on. The key difference is the default access level of members and base classes when no explicit access specifier is provided. Remembering these defaults prevents accidental exposure or hiding of APIs.


Given Data / Assumptions:

  • We are declaring members inside a class (not a struct).
  • No explicit access label (public/protected/private) precedes the members.
  • Standard C++ access rules apply.


Concept / Approach:

For a class, the default access specifier for members is private. For a struct, the default is public. This distinction also applies to base-class access in inheritance lists: class D : B implies private inheritance by default, while struct D : B implies public inheritance by default. Because the question explicitly asks about a class definition, the correct answer is private. Options like “Friend” are not access levels at all; friend is a separate language feature granting access to another function or class.


Step-by-Step Solution:

Consider class C { int x; void f(); } → x and f are private by default. Contrast with struct S { int x; }; → x is public by default. Therefore, choose “Private.”


Verification / Alternative check:

Attempt to access an unlabelled member of a class from outside; the compiler rejects it. Mark the same member in a struct; it becomes accessible. Add public: to the class and access now succeeds, confirming the default.


Why Other Options Are Wrong:

Protected/Public — these are valid access levels, but they are not the defaults for class.

Friend — not an access specifier; it is a separate friendship declaration mechanism.


Common Pitfalls:

  • Assuming struct and class differ fundamentally; they differ only in defaults.
  • Forgetting to mark intended public APIs in classes with public:.


Final Answer:

Private

More Questions from Objects and Classes

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion