In modular C++ design, using a Wardrobe structure inside a ShopList structure demonstrates which good programming principle?

Difficulty: Easy

Correct Answer: reusability

Explanation:


Introduction / Context:
Large programs benefit from composing complex types out of simpler, well-tested components. When a structure (Wardrobe) is embedded within another structure (ShopList), the design encourages code and data-model reuse. This practice aligns with modularity and helps avoid duplication.


Given Data / Assumptions:

  • C++ structures (or classes) can contain members that are other structures.
  • Wardrobe is already designed and tested.
  • ShopList needs Wardrobe-like capability and embeds it rather than reinventing it.


Concept / Approach:

  • Reusability means leveraging existing components in new contexts.
  • Polymorphism concerns substituting objects via a common interface (virtuals/templates), not mere embedding.
  • Redundancy is the opposite of good design; it duplicates logic.
  • Recursion is a control-flow technique where a function calls itself.


Step-by-Step Solution:

Identify the relationship: ShopList has-a Wardrobe → composition.Composition demonstrates reuse of Wardrobe's fields/behaviors.Therefore, the principle highlighted is reusability through composition.


Verification / Alternative check:

Compare to inheritance or duplication. Composition avoids duplicating code and keeps responsibilities separated, which confirms reusability.


Why Other Options Are Wrong:

  • Polymorphism: Not implied by simple embedding.
  • Redundancy: Not a benefit; it is what we avoid.
  • Recursion: Unrelated to data structure composition.
  • None of the above: Incorrect since reusability applies.


Common Pitfalls:

  • Overusing inheritance where composition (and reuse) is more appropriate.
  • Copy-pasting similar fields instead of embedding a reusable type.


Final Answer:

reusability

Discussion & Comments

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