When people talk about polymorphism in C++ they usually mean the thing of using a derived class through the base class pointer or reference, which is called subtype polymorphism. But they often forget that there are all kinds of other polymorphisms in C++, such as parametric polymorphism, ad-hoc polymorphism and coercion polymorphism. These polymorphisms also go by different names in C++,Subtype polymorphism is also known as runtime polymorphism.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
In this article I'll illustrate all the polymorphisms through examples in C++ language and also give insight on why they have various other names.Subtype Polymorphism (Runtime Polymorphism) Subtype polymorphism is what everyone understands when they say "polymorphism" in C++. It's the ability to use derived classes through base class pointers and references.Parametric Polymorphism (Compile-Time Polymorphism) Parametric polymorphism provides a means to execute the same code for any type. In C++ parametric polymorphism is implemented via templates.Ad-hoc Polymorphism (Overloading)Ad-hoc polymorphism allows functions with the same name act differently for each type. For example, given two ints and the + operator, it adds them together. Given two std::strings it concatenates them together. This is called overloading. Read more: Peteris Krumins' blog
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
In this article I'll illustrate all the polymorphisms through examples in C++ language and also give insight on why they have various other names.Subtype Polymorphism (Runtime Polymorphism) Subtype polymorphism is what everyone understands when they say "polymorphism" in C++. It's the ability to use derived classes through base class pointers and references.Parametric Polymorphism (Compile-Time Polymorphism) Parametric polymorphism provides a means to execute the same code for any type. In C++ parametric polymorphism is implemented via templates.Ad-hoc Polymorphism (Overloading)Ad-hoc polymorphism allows functions with the same name act differently for each type. For example, given two ints and the + operator, it adds them together. Given two std::strings it concatenates them together. This is called overloading. Read more: Peteris Krumins' blog
0 comments:
Post a Comment