CS246 Lecture 17: Lesson 17

52 views11 pages

Document Summary

Depending on pointer type, the object will behave differently, as the compiler uses the type of the pointer to decide which function to run. Compiler uses the type of the pointer (or type of the reference) to decide which method to run. It does not consider the actualtype of the object. So a comic is a comic only if a comic pointer points to it. Declaring the function virtualwithin the superclass with an overrideparameter in the subclass. To ensure correct version of functions are called. Solution: declare the method virtual class book { Virtual methods: chosen based on the actualtypes of the object runtime. Dynamic dispatch: the process of virtual methods being resolved to the correct one at runtime is known as dynamic dispatch. Process of choosing which method to call at runtime based on the type of a value. We use this by putting virtual in the superclass.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers