Computer Science 1037A/B Lecture Notes - Lecture 40: Polymorphic Code, Virtual Function, Compile Time
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.