From the course: Object-Oriented Programming with C++
Why reinvent the wheel? - C++ Tutorial
From the course: Object-Oriented Programming with C++
Why reinvent the wheel?
- [Instructor] In our previous videos, we've built several classes from scratch. Each time, we defined all the properties and behaviors those classes needed, but sometimes different classes share common characteristics. Let me show you what I mean. I've prepared the project in the chapter 3 03 01, end folder. Remember our review class? Now, imagine we need to add support for different types of reviews, say product reviews and restaurant reviews. Here we have our product review class. If you take a closer look, you will instantly notice it looks familiar to our review class, except for a couple of new member variables. We've also got the restaurant review class. This one also resembles the review class, except for a few details. These three classes have a lot in common. They all store a rating, a title, and text. They include methods to display details, access and modify the ratings, text, title, and the other specific member variables. And they all perform similar validations when updating their private data. There's a significant amount of overlapping code in these classes. This is a textbook example of violating the don't repeat yourself principle. Writing the same code multiple times is bad because, well, we have to type more, but it also means that we'll have to fix the same bugs in multiple places. We might fix it in one class, but forget to do so in the other two. You get the idea. Making changes becomes error-prone, not to mention testing the same functionality repeatedly. We could merge the common and specific code from the review, product review, and restaurant review classes into a new class called multipurpose review. This way, there is no more redundant code, and the multipurpose review provides all the combined functionality of the three classes. But this approach has serious problems. The class becomes bloated with fields that aren't always used. For example, if we want to review a product, the cuisine and has delivery fields are meaningless. Member functions specific to one type of review are visible to all reviews. Adding a new type of review like movie review would make the situation even worse. With every added type, the class keeps growing. And as it tries to handle so many different things, it violates the single responsibility principle, which states that a class should have one well-defined responsibility. There must be a better way to share common functionality while keeping specific features separate. In the following video, we'll see how C++ provides an elegant solution to this problem through inheritance.
Contents
-
-
-
-
-
Why reinvent the wheel?3m 12s
-
(Locked)
Extending classes4m 37s
-
(Locked)
Access control in inheritance4m 1s
-
(Locked)
Overriding and extending functionality2m 11s
-
(Locked)
Multiple inheritance2m 22s
-
(Locked)
Multiple inheritance pitfalls6m 32s
-
(Locked)
Challenge: RPG character system2m 25s
-
(Locked)
Solution: RPG character system1m 35s
-
-
-
-