Monday, November 15, 2010

Polymorphism

Both objects and operations are polymorphic in C++.

I originally thought that the answer was 'operations', but there are various kinds of polymorphisms involved, and different people have different definitions. Polymorphism comes from the greek meaning 'many forms'. Below are some definitions taken from the references below. For more information see the references.

Booch's Definition regards a name representing objects of different classes. Representing at any one time, one of several subclasses or the base class.

Bertrand Meyer states that the type of an object never changes, only the reference to it.

Stroustrup's Definition: the ability to call different functions with the same interfaces, as provided by virtual functions.

Rumbaugh's Definition: same operation may behave differently on different classes.

The following lists some support for the different types of answers.

operation - overloading of functions. The function name is the same, but the actual function that is called depends on the argument types. This is sometimes called ad hoc polymorphism.

operation - different virtual functions being called based on the type of the object the function is being called on. This is sometimes called dynamic polymorphism.

objects - Different objects based template parameters. For example, a Stack<int> versus Stack<float> This is a compile-time form of polymorphism. This is sometimes called static polymorphism. Templated operations are also a part of static polymorphism.

objects - pointers and references to a base type may at different times in the running of a program may point to different subclasses of the base type.

References:
   C++ Templates: The complete guide by David Vandevoorde, and Nicolai M. Josuttis, Addison-Wesley, 2003, p. 514.
   http://stason.org/TULARC/software/object-oriented-programming/2-1-What-Is-Polymorphism-Typing-Object-Oriented-Technol.html
   http://stason.org/TULARC/software/object-oriented-programming/2-1-What-Is-Polymorphism-Cardelli-and-Wegner-s-Definition.html
   http://stason.org/TULARC/software/object-oriented-programming/2-1-What-Is-Polymorphism-Other-Definitions.html
   http://www.chips.navy.mil/archives/99_oct/polymorphism.htm

No comments:

Post a Comment