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.
Pure virtual functions and abstract classes - C++ Tutorial
From the course: Object-Oriented Programming with C++
Pure virtual functions and abstract classes
- [Instructor] This video, we'll discuss an issue in our review system. Let's rerun our earlier example and see what happens when objects are destroyed. Notice something wrong? We only see the review destructors being called, not the product review or restaurant review destructors. This happens because we are dealing with a polymorphic review hierarchy. The pointers to derived objects are stored in a vector of review pointers, so this is our vector of review pointers. We then delete these objects through the base class pointer here in this four loop. Now, the objects virtual method table does not include a pointer to its destructor implementation. Thus, it cause the base review classes destructor, determined at compile time. without a virtual destructor, C++ can't find the right instructor to call. Let's fix this by making reviews destructor virtual. It's a very easy fix. So now the review class's destructor is virtual. Let's rerun the code. Now that's better. We see all destructors…
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)
-
-