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.
Virtual destructors: Why, when, and how - C++ Tutorial
From the course: Object-Oriented Programming with C++
Virtual destructors: Why, when, and how
- Let's enhance our review system to handle a new requirement. We want to ensure all reviews include a sentiment analysis, a more detailed way of evaluating content beyond just the rating. However, we don't want to provide a default implementation because each specific review subclass uses a different sentiment analysis algorithm. So let's modify our review class to include this requirement. So I'll add a new public function virtual and it returns a float. Let's call it analyze sentiment. And I'll include the sentiment score in the output of the display details function. So this is the sentiment score and the value returned by analyzeSentiment. Now if I switch back to the header file, you'll notice this new syntax, the equal zero after the analyzeSentiment declaration. This makes it a pure virtual function, a function without implementation. Pure virtual functions serve a single purpose. Define a function requirement that derived classes must implement. Declaring a pure virtual…
Contents
-
-
-
-
-
-
(Locked)
The many faces of objects3m 22s
-
(Locked)
Virtual functions: The basics2m 32s
-
(Locked)
Runtime polymorphism4m 33s
-
(Locked)
Pure virtual functions and abstract classes2m 7s
-
(Locked)
Virtual destructors: Why, when, and how6m 27s
-
(Locked)
Challenge: Superhero ability system1m 58s
-
(Locked)
Solution: Superhero ability system2m 16s
-
(Locked)
-
-