Chapter 12: Problem 3
Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BasePlusCommissionEmployee of the CommissionEmployeeBasePlusCommissionEmployee hierarchy to use composition rather than inheritance. After you do this, assess the relative merits of the two approaches for designing classes commissionEmployee and BasePlusCommissionEmployee, as well as for object-oriented programs in general. Which approach is more natural? Why?
Short Answer
Step by step solution
Understand Inheritance
Problem with Inheritance
Understanding Composition
Rewriting with Composition
Create Composition Structure
Assessing Relative Merits of Both Approaches
Conclusion on Preferred Approach
Unlock Step-by-Step Solutions & Ace Your Exams!
-
Full Textbook Solutions
Get detailed explanations and key concepts
-
Unlimited Al creation
Al flashcards, explanations, exams and more...
-
Ads-free access
To over 500 millions flashcards
-
Money-back guarantee
We refund you if you fail your exam.
Over 30 million students worldwide already upgrade their learning with Vaia!
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
inheritance in C++
The purpose of inheritance is to promote code reusability and establish a logical hierarchy between classes. For example, in a payroll system, a `BasePlusCommissionEmployee` could inherit from a `CommissionEmployee`. This means `BasePlusCommissionEmployee` will automatically have the characteristics of a `CommissionEmployee`, such as calculating commissions. However, inheritance suggests a strong "is a" relationship, implying that a `BasePlusCommissionEmployee` is a type of `CommissionEmployee`, which may not always be logical or appropriate.
- Pros: Simplifies the code by reducing redundancy, easy to understand relationships between objects.
- Cons: Can introduce rigidity due to tight coupling, which makes future changes and testing difficult.
composition in C++
For instance, instead of a `BasePlusCommissionEmployee` inheriting from `CommissionEmployee`, it can include a `CommissionEmployee` as a member object. This means that `BasePlusCommissionEmployee` uses the services of `CommissionEmployee` to perform its calculations but retains its own identity and can add new functionalities independently. With this approach, the two classes are not tightly bound, allowing changes in one without affecting the other significantly.
- Pros: Greater flexibility, easier to maintain and extend, components are more reusable.
- Cons: Can lead to more verbose code as interactions between class members need explicit handling.
class design concepts
An effective class design not only tackles current needs but anticipates future adjustments and extensions. Here are some guiding principles:
- Encapsulation: Enclosing data and methods within a class, restricting direct access from outside.
- Single Responsibility: Each class should have a single purpose or responsibility, ensuring clarity and simplicity.
- Open/Closed Principle: Design classes that are open for extension but closed for modification, enhancing adaptability without altering existing codebase.
- DRY (Don't Repeat Yourself): Avoid redundancy by ensuring each piece of knowledge has a single, unambiguous place within the system.