Computer Science 1037A/B Lecture Notes - Lecture 40: Polymorphic Code, Virtual Function, Compile Time

43 views2 pages

Document Summary

Polymorphic code: code that behaves differently when it acts on objects of different types. Virtual member function: the c++ mechanism for achieving polymorphism. Virtual functions allow the most specific version of a member function in an inheritance hierarchy to be selected for execution. Consider the animal, cat, dog hierarchy where each class has its own version of the member function id( ) Consider the collection of different animal objects and accompanying code. Prints: animal animal animal, ignoring the more specific versions of id() in dog and cat. The preceding code is not polymorphic: it behaves the same way even though animal, dog and cat have different types and different id() member functions. Polymorphic code would have printed "animal dog cat" instead of "animal animal animal" The code is not polymorphic because in the expression the compiler sees only the type of the pointer pa[k], which is pointer to.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents