OOP Composition Exercises in C++
OOP Composition Exercises in C++
Object initialization methods vary by encapsulating relevant attributes via constructors. The Car class initializes its Engine inline, hiding specific engine initial state details . The House class uses a vector constructor to individually add Room objects . The Computer class initializes the Processor using a specific speed value, showing parameterized construction . Despite differences, all employ constructor functions to encapsulate initialization logic, ensuring the objects start in a valid state by default .
The 'has-a' relationship, also known as composition, is shown when one class contains objects of another class. For example, the Car class 'has-a' Engine as a member variable, meaning the Car relies on its Engine to function, but both remain distinct entities . This relationship indicates that the Car forms a whole, composed of an Engine, allowing separate lifecycle management of the components .
Encapsulation in the Book and Library classes is implemented by using private member variables and public methods. In the Book class, properties like 'title' and 'author' are private, ensuring they cannot be directly accessed from outside the class. Instead, methods are provided for interaction, such as 'display()' to show book details . The Library class controls access to its collection using methods like 'addBook()' and 'removeBook()', thus maintaining and modifying its state securely .
In OOP, composition allows for building complex objects by combining simpler ones. This promotes reusability because individual components, like the Engine in the Car exercise, can be utilized across different systems without rewriting code . It enhances flexibility as it supports object customization by altering component combinations, thus adapting to varying requirements without affecting the entire system .
The separation of concerns is achieved by assigning specific responsibilities to distinct classes. The Processor class focuses on managing attributes about processor speed and methods related to it, such as 'displaySpeed()'. Meanwhile, the Computer class is responsible for the overall computer specifications, incorporating a Processor object to fulfill its role of displaying detailed specs . This design encapsulates functionalities in coherent modules, reducing interdependence and enhancing maintainability .
Removing a book by title in the Library class can be challenging if multiple books have the same title or if the title is not present. This could lead to incorrect removals or failures to remove the intended book. A potential solution is to enhance the method to also check for unique identifiers (e.g., book ID) or author names to accurately locate the book in the collection .
Using public methods like 'display()' in the Room class allows external entities to access and utilize room details openly, adhering to encapsulation by protecting access to underlying data. Alternatives might involve providing getter methods to retrieve data for display externally, facilitating customization of display logic by the calling function. This variation could allow for more flexible UI integrations, albeit demanding greater control from the user over presentation logic .
Using vectors to store objects like Books or Rooms can introduce memory management issues, especially with large collections, as vectors dynamically resize which could lead to fragmented memory. Furthermore, sequential searches in large vectors can be inefficient. These issues might be mitigated by using alternative data structures like maps for faster lookups or employing smart pointers to manage memory more efficiently .
Encapsulating engine status within the Engine class contributes to system robustness by protecting the state integrity from unauthorized changes, restricting modifications to controlled methods like 'start()' or 'stop()'. This prevents inconsistent states in the Car class that relies on the Engine. Moreover, encapsulation leads to efficiency in maintenance as changes to engine behavior are localized to the Engine class without affecting other system parts .
Composing a Book with an Author using direct object composition ensures the Book includes complete Author information, simplifying data management, and ensuring no null references. This can be advantageous for ease of access and managing fixed relationships . However, it might lead to inefficiencies if multiple books by the same author exist, as Author details would be duplicated across Books. Instead, using pointers could reduce memory usage by sharing Author information among Books, though it introduces potential risks of dangling pointers or null references .