Difficulty: Easy
Correct Answer: Incorrect — it is legal and common (composition).
Explanation:
Introduction / Context:
This tests understanding of composition, a fundamental OO design technique where a class contains objects of other classes as members. C++ fully supports composition and relies on it for RAII and aggregation patterns. Declaring member objects is not only legal; it is idiomatic.
Given Data / Assumptions:
Concept / Approach:
Composition embeds an object directly inside another object’s storage. Construction/destruction of member subobjects are automatically sequenced: members are constructed before the containing object’s body runs and destroyed in reverse order. Whether the member has a default constructor matters only for default-initialization; if it lacks one, you must initialize it explicitly in the owner’s member-initializer list. None of this makes composition illegal.
Step-by-Step Solution:
Verification / Alternative check:
Try implementing a class that owns a std::string or std::vector. These are objects from the standard library; their presence as members exemplifies composition working seamlessly.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting to initialize non-default-constructible members in the initializer list; assuming composition implies ownership semantics identical to pointers (RAII usually makes it safer).
Final Answer:
Incorrect — it is legal and common (composition).
Discussion & Comments