Difficulty: Easy
Correct Answer: Static
Explanation:
Introduction / Context:
Class design often distinguishes between per-object state and per-class state. When a value should be common across all objects (for example, a counter of instances or a configuration flag), C++ provides a specific storage class and access pattern to model this: static data members. Understanding this distinction avoids redundant storage and keeps invariants centralized.
Given Data / Assumptions:
Concept / Approach:
A static data member is allocated once and shared among all instances of the class. It can be accessed via the class name (e.g., T::count) or through an object (though the former is clearer). Public/private affects visibility, not multiplicity. “Inherited” is not a storage class; it merely describes where names come from in hierarchies. “Friend” is an access grant, not a data member category. Hence, the only option that encodes “shared by all instances” is static.
Step-by-Step Solution:
Verification / Alternative check:
Instantiate several objects and modify a static member through one of them; observe the updated value through all others and via the class name, confirming shared storage.
Why Other Options Are Wrong:
Public — concerns visibility, not sharing.
Inherited — not a storage specifier.
Friend — grants access to another function/class; not a member kind.
Common Pitfalls:
Final Answer:
Static
Discussion & Comments