{"id":235,"date":"2020-09-01T17:32:25","date_gmt":"2020-09-01T17:32:25","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=235"},"modified":"2020-09-01T17:32:25","modified_gmt":"2020-09-01T17:32:25","slug":"c-oop-polymorphism","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/","title":{"rendered":"C++ OOP &#8211; Polymorphism"},"content":{"rendered":"<p>As a word, <strong>Polymorphism<\/strong> means &#8216;many forms&#8217;. In OOP, it arise when there are classes that are related to each other via <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-oop-inheritance\/\" target=\"_blank\" rel=\"noopener noreferrer\">inheritance<\/a>.<\/p>\n<p>So polymorphism in C++ means that a function with the same name exists in different related classes. Therefore, when a call is made, the particular function that would be executed would depend on the object that invokes it.<\/p>\n<p>Let&#8217;s take for example, a class Fruit. Then three classes derive from fruit: Banana, Apple and Grape. Then each have a function: describe().<\/p>\n<p>This is shown below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #888888;\">\/\/ Base class Fruit<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Fruit<\/span> {\r\n<span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n\t<span style=\"color: #333399; font-weight: bold;\">void<\/span> describe(){\r\n\t\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Delicious and good for the health\"<\/span>;\r\n\t}\r\n};\r\n\r\n\r\n<span style=\"color: #888888;\">\/\/ Derived class, Banana<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Banana<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> Fruit {\r\n\t<span style=\"color: #333399; font-weight: bold;\">void<\/span> describe(){\r\n\t\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Banana: Slender. Yellow when ripe\"<\/span>;\r\n\t}\r\n};\r\n\r\n\r\n<span style=\"color: #888888;\">\/\/ Derived class, Orange<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Orange<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> Fruit {\r\n\t<span style=\"color: #333399; font-weight: bold;\">void<\/span> describe(){\r\n\t\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Orange: Round and juicy\"<\/span>;\r\n\t}\r\n};\r\n\r\n\r\n<span style=\"color: #888888;\">\/\/ Derived class, Grape<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Grape<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> Fruit {\r\n\t<span style=\"color: #333399; font-weight: bold;\">void<\/span> describe(){\r\n\t\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Grape: Used to make wine\"<\/span>;\r\n\t}\r\n};\r\n<\/pre>\n<p>In the example above, the describe() member function is called a <em><strong>polymorphous function<\/strong><\/em>.<\/p>\n<p>Now we have created all the classes. We can then create objects from them and override the describe() in the base case. <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-oop-function-overriding\/\" target=\"_blank\" rel=\"noopener noreferrer\">See Function Overriding<\/a><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Calling Polymorphous functions<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\r\n\t<span style=\"color: #888888;\">\/\/Create different objects<\/span>\r\n\tFruit myFruit;\r\n\tBanana myBanana;\r\n\tOrange myOrange;\r\n\tGrape myGrape;\r\n\r\n\tmyFruit.describe();\r\n\tmyBanana.describe();\r\n\tmyOrange.describe();\r\n\tmyGrape.describe();\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>The output of the program is given below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Delicious and good for the health \r\nBanana: Slender. Yellow when ripe \r\nOrange: Round and juicy \r\nGrape: Used to make wine \r\n<\/pre>\n<p>So we can see that the polymorphous functions are called depending on the object that makes the call.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a word, Polymorphism means &#8216;many forms&#8217;. In OOP, it arise when there are classes that are related to each other via inheritance. So polymorphism &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,35],"tags":[41],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","category-oop","tag-polymorphism"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/235\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}