Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Polymorphism

 

C++ Polymorphism

Introduction

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function 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 namely


  • 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.



Read Next: C++ Virtual Functions



 
Related Topics


 

Comments


anup josi said:

  i m highly impress by ur view........................but you shoud have to explain more in this topic.....................polymorphism is a important topic in c .
September 7, 2007, 3:50 am

torsak said:

  Thank you; it really helps in understanding the polymorphism concept !!
April 20, 2008, 5:41 am

Mani Prabha said:

  It's nice to get to know about all the types of polymorphisms but it would be nice if you could exaplain each with an example as it is a very important topic and is implemented in most cases.
June 12, 2008, 1:07 am

Selva Kumar said:

  Its very good and It is very useful to my Tomoorow's seminar.............................
September 28, 2008, 2:21 pm

Rahul said:

  this site is helping me clear many small concept problems.
and happy to find this site.
November 4, 2008, 12:49 pm

satish kumar said:

  Its content is highly conceptual..and this is going to help me to answer in company interview pannel
November 6, 2008, 3:42 am

Rajesh kumar said:

  explanation is very nice but it would more clear for me,if you give some example based on your concepts...
November 8, 2008, 5:17 am

Arun Kumar said:

  thanks 2maro is my seminar 0n this topic & i got sufficient info. from this. thanx agian
November 10, 2008, 3:43 am

satheesh reddy . a said:

  thanks to exforsys, this site is helping to learn c++ basic
November 13, 2008, 6:33 am

Bhupinder Singh said:

  it is sufficient for me to explain polymorphism to students..................now i will explain this topic to all students in my tomarrow's seminar
November 26, 2008, 6:17 am

REKHA SONI said:

  Thanks it is important for me in my exam with nice examples
November 30, 2008, 10:48 am

pankaj verma said:

  thank god u help me by that type concept on ur site
fully mind blowing concept clearing
December 10, 2008, 6:15 pm

pratap dixit said:

  Thanks for your article.It covers overall in good way. It would be more better if it explains example for each regarding "other kinds of polymorphisms" .
December 18, 2008, 7:15 am

Johnson J K said:

  Short and Sweet, just the way i like it. Thanks for the explanation, this sites going into my project :)
January 6, 2009, 7:36 am

Deepak H said:

  Thanks for good article.It would be better if u explain with example for each concept.
January 21, 2009, 3:58 am

Ravindar said:

  The concept is explained well, but I still have a question whether the Operator Overloading is a compile-time polymorphism or run-time polymorphism. Thanks in advance.
January 21, 2009, 9:40 am

shivani said:

  this concept is vry gud ...... but plz explain more abt run-time polymorphism and compile-time plymorphism
February 1, 2009, 6:41 am

Vishal Kumar said:

  Please elaborate ad hoc polymorphism and parametric polymorphism more by giving some examples.
Thanks for this useful technical article.
May 24, 2009, 8:50 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape