In object-oriented C++, which feature primarily provides a code-reuse mechanism? Select the concept most directly associated with reusing existing implementations.

Difficulty: Easy

Correct Answer: Inheritance

Explanation:


Introduction:
Code reuse is a central goal of object-oriented programming. While several OOP principles contribute indirectly, one feature most directly enables reuse of implemented behavior across related types.


Given Data / Assumptions:

  • Language: C++.
  • Typical OOP toolkit: abstraction, encapsulation, inheritance, and polymorphism (dynamic binding).
  • Focus: direct mechanism for implementation reuse.


Concept / Approach:
Inheritance allows a derived class to reuse and extend the implementation of a base class, inheriting data and member functions. Although abstraction and encapsulation improve design clarity and safety, inheritance explicitly shares implemented behavior. Dynamic binding enables substitutability, not reuse per se.


Step-by-Step Solution:
1) Identify reuse goal: avoid re-writing common behavior.2) Map OOP features to outcomes: encapsulation hides details; abstraction models concepts; dynamic binding chooses implementations at runtime.3) Recognize that inheritance copies base interface and implementation into the derived type’s interface/behavior.4) Therefore, inheritance is the direct reuse mechanism.


Verification / Alternative check:
Common frameworks provide abstract base classes with default implementations (e.g., adapter classes) that derived classes reuse and override selectively.


Why Other Options Are Wrong:
Abstraction: defines essential interfaces; does not itself reuse code.Dynamic binding: enables polymorphism, not implementation reuse.Encapsulation: controls access/visibility; does not replicate implementation across types.


Common Pitfalls:
Overusing inheritance where composition would be better. Prefer “composition over inheritance” when reuse does not require an is-a relationship.


Final Answer:
Inheritance

More Questions from OOPS Concepts

Discussion & Comments

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