Friday 16 November 2012

C++ Polymorphism

C++ Polymorphism

Introduction

Polymorphism is the ability to use an operator or method in different ways. Polymorphism gives different meanings or functions to the operators or methods. Poly, referring to many, signifies the many uses of these operators and methods. A single method usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
Below is a simple example of the above concept of polymorphism:
6 + 10
The above refers to integer addition.
The same + operator can be used with different meanings with strings:
"Exforsys" + "Training"
The same + operator can also be used for floating point addition:
7.15 + 3.78
Polymorphism is a powerful feature of the object oriented programming language C++. A single operator + behaves differently in different contexts such as integer, float or strings referring the concept of polymorphism. The above concept leads to operator overloading. The concept of overloading is also a branch of polymorphism. When the exiting operator or function operates on new data type it is overloaded. This feature of polymorphism leads to the concept of virtual methods.
Polymorphism refers to the ability to call different functions by using only one type of function call. Suppose a programmer wants to code vehicles of different shapes such as circles, squares, rectangles, etc. One way to define each of these classes is to have a member function for each that makes vehicles of each shape. Another convenient approach the programmer can take is to define a base class named Shape and then create an instance of that class. The programmer can have array that hold pointers to all different objects of the vehicle followed by a simple loop structure to make the vehicle, as per the shape desired, by inserting pointers into the defined array. This approach leads to different functions executed by the same function call. Polymorphism is used to give different meanings to the same concept. This is the basis for Virtual function implementation.
In polymorphism, a single function or an operator functioning in many ways depends upon the usage to function properly. In order for this to occur, the following conditions must apply:
  • All different classes must be derived from a single base class. In the above example, the shapes of vehicles (circle, triangle, rectangle) are from the single base class called Shape.
  • The member function must be declared virtual in the base class. In the above example, the member function for making the vehicle should be made as virtual to the base class.

Features and Advantages of the concept of Polymorphism:

Applications are Easily Extendable:

Once an application is written using the concept of polymorphism, it can easily be extended, providing new objects that conform to the original interface. It is unnecessary to recompile original programs by adding new types. Only re-linking is necessary to exhibit the new changes along with the old application. This is the greatest achievement of C++ object-oriented programming. In programming language, there has always been a need for adding and customizing. By utilizing the concept of polymorphism, time and work effort is reduced in addition to making future maintenance easier.
  • Helps in reusability of code.
  • Provides easier maintenance of applications.
  • Helps in achieving robustness in applications.

Types of Polymorphism:

C++ provides three different types of polymorphism.
  • Virtual functions
  • Function name overloading
  • Operator overloading
In addition to the above three types of polymorphism, there exist other kinds of polymorphism:
  • run-time
  • compile-time
  • ad-hoc polymorphism
  • parametric polymorphism
Other types of polymorphism defined:

run-time:

The run-time polymorphism is implemented with inheritance and virtual functions.

compile-time:

The compile-time polymorphism is implemented with templates.

ad-hoc polymorphism:

If the range of actual types that can be used is finite and the combinations must be individually specified prior to use, this is called ad-hoc polymorphism.

parametric polymorphism:

If all code is written without mention of any specific type and thus can be used transparently with any number of new types it is called parametric polymorphism.
In general, there are two main categories of Polymorphism:
  • Ad Hoc Polymorphism
  • Pure Polymorphism

Overloading concepts fall under the category of Ad Hoc Polymorphism and Virtual methods. Templates or parametric classes fall under the category of Pure Polymorphism

No comments:

Post a Comment