From the course: Object-Oriented Programming with C++

Unlock this course with a free trial

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

Extending classes

Extending classes

- [Instructor] Looking at our project files, we can see the massive code duplication across our three classes. Let's fix this using inheritance. First, we need to identify what these classes have in common. All three classes share a common set of Review data, the rating, the title, and the text. They also have the same displayDetails public member function plus the getters and the setters for the rating, title, and text member variables. They also share the same validation logic for rating range and string lengths. If we check the implementation of the Review and the ProductReview, the setRating performs the same validations, and in setTitle and setText they call validateAndTrim. Now, our Review class already has all this functionality well implemented. It will serve as our base class, what we call a parent class in inheritance. Now, let's modify ProductReview to inherit from Review. Let me close these files and we'll open the header. First, we must include the review header…

Contents