



{"id":4610,"date":"2018-09-06T15:49:37","date_gmt":"2018-09-06T10:19:37","guid":{"rendered":"http:\/\/computersirkiclass.com\/?p=4610"},"modified":"2026-03-18T17:42:15","modified_gmt":"2026-03-18T12:12:15","slug":"concept-of-polymorphism-in-c","status":"publish","type":"post","link":"https:\/\/computersirkiclass.com\/cskc\/concept-of-polymorphism-in-c\/","title":{"rendered":"Concept of Polymorphism in C++"},"content":{"rendered":"<div id=\"modal-ready\"><p>Let us begin with understanding of the word polymorphism. The word has a greek origin from words polus\/polloi and morphe. &#8216;Poly&#8217; means &#8216;many&#8217; and &#8216;Morph&#8217; means forms. So when any entity has many forms according to its context of use, we may call it polymorphism. This nomenclature is frequently used in biological studies. For e.g. Hepatitis virus is said to be polymorphic as it has many strains.<\/p>\n<p>&nbsp;<\/p>\n<p>In english language the homonyms words are also a form of polymorphism only. For e.g. the word &#8220;date&#8221; can have different meaning in different contexts. This is clear from the following diagram.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_4635\" style=\"width: 317px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4635\" class=\"wp-image-4635 size-full\" src=\"http:\/\/computersirkiclass.com\/wp-content\/uploads\/2018\/09\/date-1.png\" alt=\"\" width=\"307\" height=\"160\" srcset=\"https:\/\/computersirkiclass.com\/cskc\/wp-content\/uploads\/2018\/09\/date-1.png 307w, https:\/\/computersirkiclass.com\/cskc\/wp-content\/uploads\/2018\/09\/date-1-300x156.png 300w\" sizes=\"auto, (max-width: 307px) 100vw, 307px\" \/><p id=\"caption-attachment-4635\" class=\"wp-caption-text\"><center>Fig 1: English homonyms depicting polymorphism<center><\/center><\/center><\/p><\/div>\n<p>&nbsp;<\/p>\n<p>Let us take an example of computer science. Suppose we write<\/p>\n<p>&nbsp;<\/p>\n<pre>a=5; b=7; int c=a+b; \/\/result in c would be integer addition of a and b which would be 12\r\n\r\n\/\/ in case of\r\nstring x=\"poly\"; string y=\"morphism\";\r\nstring z=a+b; \/\/result in z would be string concatenation as polymorphism<\/pre>\n<p>&nbsp;<\/p>\n<p>Here the + (addition) operator takes two different forms. First one to add numbers and second one to concatenate strings.<\/p>\n<p>&nbsp;<\/p>\n<p>The same example can be diagramatically understood as follows.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_4634\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4634\" class=\"wp-image-4634 size-full\" src=\"http:\/\/computersirkiclass.com\/wp-content\/uploads\/2018\/09\/operator-1.png\" alt=\"\" width=\"490\" height=\"162\" srcset=\"https:\/\/computersirkiclass.com\/cskc\/wp-content\/uploads\/2018\/09\/operator-1.png 490w, https:\/\/computersirkiclass.com\/cskc\/wp-content\/uploads\/2018\/09\/operator-1-300x99.png 300w\" sizes=\"auto, (max-width: 490px) 100vw, 490px\" \/><p id=\"caption-attachment-4634\" class=\"wp-caption-text\"><center>Fig. 2: Operator + being used in two different forms<\/center><\/p><\/div>\n<p>&nbsp;<\/p>\n<p>Let us understand it a bit further. Suppose we write two functions as follows.<\/p>\n<pre>float add(float x, float y); \u00a0\u00a0\/\/Definition 1\r\n\r\nint add(int a, int b);\u00a0 \/\/Definition 2<\/pre>\n<p>&nbsp;<\/p>\n<p>Here the internal implementation code of these functions will definitely be different as the way to add integer numbers and fractional numbers are different. This is typically known as function overloading. In a way it is also polymorphism.<\/p>\n<p>&nbsp;<\/p>\n<p>When we explore further we will come across more dimensions of this term polymorphism in context of computer science. Some times experts differ on exact definition of polymorphism. For instance in the above example of function overloading compiler is able to create two separate code bodies for each of the parameter types and in real calls to the code, references are not really dynamic in nature. If call is with float parameters the code portion referenced by definition 1 is reached and if the call is with integer parameters code portion referenced by definition 2 is reached.<\/p>\n<p>&nbsp;<\/p>\n<p>So let us classify polymorphism on the basis of its dynamism.<\/p>\n<ul>\n<li>Compile time polymorphism (Static Polymorphism)<\/li>\n<li>Run time polymorphism (Dynamic Polymorphism)<\/li>\n<\/ul>\n<h3>Compile Time Polymorphism (Static)<\/h3>\n<p>In this type of polymorphism the names of called entities (functions, operators or variables) are same but compiler is able to identify the differences and use it as per the input parameters or other input conditions. In this type the entities may appear to be polymorphic but in real sense at run time they have clearly defined separate references. For this reason experts call it as <strong>adhoc polymorphism<\/strong> or sometimes they do not consider at polymorphism.<\/p>\n<p>&nbsp;<\/p>\n<p>The example of operator with overloaded use and function as overloaded functions given above come under this category.<\/p>\n<h3>Run Time Polymorphism (Dynamic)<\/h3>\n<p>In this type of polymorphism the reference code at the run time is also same and variation in execution is dependent on how it has been called. This is often the case when polymorphism is implemented in inherited classes.<\/p>\n<p>&nbsp;<\/p>\n<p>Consider the following example.<\/p>\n<pre>#include &lt;iostream&gt;\r\nusing namespace std;\r\nclass Mammal\r\n{\r\n\u00a0 public:\r\n\u00a0 virtual void fly(){cout&lt;&lt;\"Mammals don't fly\"&lt;&lt;endl;}\r\n};\r\nclass Bat:public Mammal\r\n{\r\n\u00a0 public:\r\n\u00a0 void fly(){cout&lt;&lt;\"Bats are exceptions they fly\"&lt;&lt;endl;}\r\n};\u00a0\r\n\r\nvoid Doesfly(Mammal *m) { m-&gt;fly();}\r\n\r\nint main()\r\n{ \r\n\u00a0 Bat b; Mammal m;\r\n\u00a0 Doesfly(&amp;m);\r\n\u00a0 Doesfly(&amp;b);\r\n\u00a0 return 0;\r\n}\r\n\r\nThe output of above code when run us is as follows.\r\n\r\nMammals don't fly\r\nBats are exceptions they fly<\/pre>\n<p>&nbsp;<\/p>\n<p>In the above code the function doesfly() parameters are of same type when they are called but each time they will call a different function based on which object has been passed to it. You can see that this different from function overloading. Only one function Doesfly() has been called but its impact is different.<\/p>\n<p>&nbsp;<\/p>\n<p>This is because parent Mammal has a virtual function fly() and child class Bat has a overrided function fly. Since both superclass and subclass reference is type compatible so we can always write some function (Which is doesfly() in this case) with a parameter of type Superclass and still pass the subclass to it.<\/p>\n<p>&nbsp;<\/p>\n<p>Since this selection of function call based on which reference has been passed happens at the run time, we call it <strong>runtime polymorphism<\/strong> or <strong>dynamic polymorphism<\/strong>. Some times people also call it <strong>subtype polymorphism<\/strong>.<\/p>\n<h2>Other Types of Polymorphism<\/h2>\n<h3>Coercion Polymorphism<\/h3>\n<p>Sometimes people call typecasting also as a type of polymorphism only. For e.g. if I say<\/p>\n<pre>double a=12.5;\r\nint b=(int) a;<\/pre>\n<p>&nbsp;<\/p>\n<p>In the above case we can clearly see that variable a will exist in two forms. One in the double form and another in the integer form due to the casting operator (int) applied to it. So this in a way is also aform of polymorphism only.<\/p>\n<h3>Parametric Polymorphism<\/h3>\n<p>In C++ this type of polymorphism although not implemented in its Ideal form can still be indirectly achieved. Description is currently out of scope of this article, but interested learners can still try to follow the links given here.<\/p>\n<p><a href=\"http:\/\/www.catonmat.net\/blog\/cpp-polymorphism\/\">http:\/\/www.catonmat.net\/blog\/cpp-polymorphism\/<\/a><\/p>\n<p><a href=\"http:\/\/www.cplusplus.com\/doc\/tutorial\/polymorphism\/\">http:\/\/www.cplusplus.com\/doc\/tutorial\/polymorphism\/<\/a><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Parametric_polymorphism\">https:\/\/en.wikipedia.org\/wiki\/Parametric_polymorphism<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Let us begin with understanding of the word polymorphism. The word has a greek origin from words polus\/polloi and morphe. &#8216;Poly&#8217; means &#8216;many&#8217; and &#8216;Morph&#8217; means forms. So when any entity has many forms according to its context of use, we may call it polymorphism. This nomenclature is frequently used in biological studies. For e.g. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7763,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[114,99,98],"tags":[238],"model_paper":[],"class_list":["post-4610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cnr","category-cpp","category-cpp-all","tag-cp370-polymorphism","cpp_index-cpa030-polymorphism"],"acf":[],"_links":{"self":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/comments?post=4610"}],"version-history":[{"count":3,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4610\/revisions"}],"predecessor-version":[{"id":4642,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4610\/revisions\/4642"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/media\/7763"}],"wp:attachment":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/media?parent=4610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/categories?post=4610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/tags?post=4610"},{"taxonomy":"model_paper","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/model_paper?post=4610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}