Levels of Polymorphism

Budd

C++

Polymorphic Variables
    Any variable whose static type and dynamic type need not agree
Only pointers and reference variables have the potential for polymorphism.
Overloading
  • Overloading of function names
  • Parametric overloading of functions
Overloading
  • Overloading of function names - could have the same function name in multiple scopes - draw() in card class, draw() in shape class, etc.
  • Parametric overloading of functions - multiple functions with the same name in the same scope with different parameters - swap() examples. NOTE: parametric overloading and parameterized types are NOT the same thing. Perry/Levin refer to parameterized types in conjunction with templates.
Overriding - functions in both the parent and child which have the same name and same parameters
  • Refinement - as part of the child's code, the parent's code is executed
  • Replacement - none of the parent's code is executed.
Overriding - same as Budd's description for overriding for refinement and replacement with a couple of extra considerations
  • non-virtual overriding - the static type of the accessing variable whether a pointer, reference, or "regular" variable is used to determine which method (parent's or child's) is used
  • virtual overriding - the dynamic type of the accessing pointer or reference variable is used to determine which method is used. Note that the static type of the pointer or reference variable is used to determine (at compile time) whether or not a method exists.
Deferred Methods
Methods are defined in the parent but not implemented. It is up to the child class to implement the methods.
Pure virtual methods - at least one method in a parent class has a pure virtual method. The compiler requires that all children of this class define a method for any pure virtual methods. Also, it is impossible to declare a variable of the parent class. You can declare pointers or reference variables to the parent class.
Pure Polymorphism Doesn't exist in C++
Generics and Templates Templates - Chapter 14 pg. 615 in LaFore The idea is for the writer of the class(or function) to write the class (or function) as an outline. All the information is there, but the type is designated by a "dummy" name. When instantiating a template class, the user of the class will instantiate it and will specify a type to use (like int, Rational, etc.). The compiler will actually create a version of the class and will substitute the specified type for the dummy type given in the outline. This is a form of overloading. In this case, the compiler "writes" all the different versions needed instead of the programmer.