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.
Solution: Superhero ability system - C++ Tutorial
From the course: Object-Oriented Programming with C++
Solution: Superhero ability system
(upbeat music) - [Instructor] Welcome back. Here's my solution to our Ability System challenge. I started by adding a virtual destructor to the base Ability class. This is necessary to ensure proper cleanup in our polymorphic hierarchy. Without it, derived class destructors would not be called when deleting their instances through base class pointers. Next, let's look at the RangedAttack class, and here's its constructor. As you can see, I've added the missing part to its constructor member initialization list to set up the base class, not just its member variable. Now the base class part is also initialized correctly. The use function must be implemented since it's a pure virtual function in the base class. If we did not implement this method, RangedAttack would turn into an abstract class and we could not instantiate it. I've also overridden describe, which force calls the base class's describe method, and then adds its range value to the output log. Let's see the MelleAttack class,…
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)
-
-