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.
Runtime polymorphism - C++ Tutorial
From the course: Object-Oriented Programming with C++
Runtime polymorphism
- [Instructor] In our previous video, we saw how virtual functions let us write code that works with different types of reviews through Base class pointers. But what's actually happening behind the scenes? Let me explain how C++ makes this magic work. When this code runs, C++ needs to figure out which version of getRating to call, the Base Review version, the productReview version, or the RestaurantReview version. This decision happens at runtime, not when we compile the code. That's why we call it runtime polymorphism. This is different from regular function calls, where the compiler decides which function to call when compiling the code. With virtual functions, the decision is delayed until the program runs, allowing our code to work with types we might not even know about when writing it. C++ accomplishes this through a mechanism called dynamic dispatch. Note that implementation details may slightly vary across compilers, but the model we'll discuss is a good approximation of…
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)
-
-