From the course: C++ Design Patterns: Structural

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Computer shop demo using inheritance

Computer shop demo using inheritance - C++ Tutorial

From the course: C++ Design Patterns: Structural

Computer shop demo using inheritance

- [Instructor] Suppose we have a computer shop that sells different types of computers, such as desktops and laptops. We could represent these computers as classes that inherit from a base computer class. For example, the Computer abstract class could define methods for displaying the computer's price and description. The child classes Desktop and Laptop would then override these methods and provide their own implementation. We can then use these classes to create different types of computers and print out their descriptions and prices. However, there are some design problems with this approach. Suppose we want to add additional functionalities to our computers, such as upgrading the memory or adding a graphics card. We could create new classes for each combination of computer and functionality, such as desktop with graphics card and laptop with memory upgrade. This approach can quickly become unmanageable if we have…

Contents