C++ class design: what relationship is expressed by the following definitions? class Bike { Engine objEng; }; class Engine { float CC; };

Difficulty: Easy

Correct Answer: has a relationship

Explanation:


Introduction / Context:
Object-oriented design uses relationship vocabulary to describe how types relate. “Is-a” denotes inheritance (a specialized kind of the base), while “has-a” denotes composition/aggregation (an object contains or owns another object). Correctly identifying the relationship guides design choices like lifetime management and interface exposure.


Given Data / Assumptions:

  • class Bike { Engine objEng; }; — Bike has a data member of type Engine.
  • Engine is a separate class with some state (e.g., displacement in CC).
  • No inheritance is present in the snippet (no colon syntax).


Concept / Approach:

The presence of an Engine data member inside Bike indicates composition: a Bike has an Engine. This is typically a strong “has-a” relationship, often implying ownership and synchronized lifetimes (the Engine subobject is constructed and destroyed with the Bike). An “is-a” (inheritance) relationship would have used the syntax class Bike : public Engine { ... };. Vague phrases like “kind of relationship” do not correspond to standard OO terminology and are not specific enough to be correct here.


Step-by-Step Solution:

Inspect Bike: contains Engine as a direct data member → composition. No base-class list (:) → not inheritance. Therefore, relational phrase is “has a relationship.”


Verification / Alternative check:

Consider construction order: when a Bike is created, its Engine member is constructed first. On destruction, Engine is destroyed last among members. This lifecycle coupling is characteristic of composition and confirms the “has-a” judgment.


Why Other Options Are Wrong:

kind of relationship — not a standard term; too vague.

Inheritance — would require class Bike : public Engine, which is absent.

Both A and B — includes a non-standard term; only “has a relationship” fits.


Common Pitfalls:

  • Confusing aggregation (has-a, possibly non-owning) with inheritance (is-a).
  • Using public inheritance to model “has-a,” which misstates the type relationship.


Final Answer:

has a relationship

Discussion & Comments

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