Tutorials
C++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:
|
The above refers to integer addition.
The same + operator can be used with different meanings with strings:
|
The same + operator can also be used for floating point addition:
|
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:
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.
C++ provides three different types of polymorphism.
In addition to the above three types of polymorphism, there exist other kinds of polymorphism:
Other types of polymorphism defined:
The run-time polymorphism is implemented with inheritance and virtual functions.
The compile-time polymorphism is implemented with templates.
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.
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
Overloading concepts fall under the category of Ad Hoc Polymorphism and Virtual methods. Templates or parametric classes fall under the category of Pure Polymorphism.
| i m highly impress by ur view........................but you shoud have to explain more in this topic.....................polymorphism is a important topic in c . |
| Thank you; it really helps in understanding the polymorphism concept !! |
| 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. |
| Its very good and It is very useful to my Tomoorow's seminar............................. |
|
this site is helping me clear many small concept problems. and happy to find this site. |
| Its content is highly conceptual..and this is going to help me to answer in company interview pannel |
| explanation is very nice but it would more clear for me,if you give some example based on your concepts... |
| thanks 2maro is my seminar 0n this topic & i got sufficient info. from this. thanx agian |
| thanks to exforsys, this site is helping to learn c++ basic |
| it is sufficient for me to explain polymorphism to students..................now i will explain this topic to all students in my tomarrow's seminar |
| Thanks it is important for me in my exam with nice examples |
|
thank god u help me by that type concept on ur site fully mind blowing concept clearing |
| 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" . |
| Short and Sweet, just the way i like it. Thanks for the explanation, this sites going into my project :) |
| Thanks for good article.It would be better if u explain with example for each concept. |
| 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. |
| this concept is vry gud ...... but plz explain more abt run-time polymorphism and compile-time plymorphism |
|
Please elaborate ad hoc polymorphism and parametric polymorphism more by giving some examples. Thanks for this useful technical article. |