{"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:\/\/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:\/\/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:\/\/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":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"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"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ OOP - Polymorphism - C++ Tutorials<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ OOP - Polymorphism - C++ Tutorials\" \/>\n<meta property=\"og:description\" content=\"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;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/\" \/>\n<meta property=\"og:site_name\" content=\"C++ Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-01T17:32:25+00:00\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\"},\"headline\":\"C++ OOP &#8211; Polymorphism\",\"datePublished\":\"2020-09-01T17:32:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/\"},\"wordCount\":157,\"commentCount\":0,\"keywords\":[\"Polymorphism\"],\"articleSection\":[\"C++ Tutorials\",\"OOP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/\",\"name\":\"C++ OOP - Polymorphism - C++ Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#website\"},\"datePublished\":\"2020-09-01T17:32:25+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/c-oop-polymorphism\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ OOP &#8211; Polymorphism\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#website\",\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/\",\"name\":\"C++ Tutorials\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/#\\\/schema\\\/person\\\/59a11a9f72bb132f56f91ab9f8a58a0e\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/kindsonthegenius.com\\\/cplusplus\\\/author\\\/kindsonthegenius-3\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ OOP - Polymorphism - C++ Tutorials","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/","og_locale":"en_US","og_type":"article","og_title":"C++ OOP - Polymorphism - C++ Tutorials","og_description":"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;","og_url":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/","og_site_name":"C++ Tutorials","article_published_time":"2020-09-01T17:32:25+00:00","author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/#article","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e"},"headline":"C++ OOP &#8211; Polymorphism","datePublished":"2020-09-01T17:32:25+00:00","mainEntityOfPage":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/"},"wordCount":157,"commentCount":0,"keywords":["Polymorphism"],"articleSection":["C++ Tutorials","OOP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/","url":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/","name":"C++ OOP - Polymorphism - C++ Tutorials","isPartOf":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#website"},"datePublished":"2020-09-01T17:32:25+00:00","author":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e"},"breadcrumb":{"@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/c-oop-polymorphism\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kindsonthegenius.com\/cplusplus\/"},{"@type":"ListItem","position":2,"name":"C++ OOP &#8211; Polymorphism"}]},{"@type":"WebSite","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#website","url":"https:\/\/kindsonthegenius.com\/cplusplus\/","name":"C++ Tutorials","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kindsonthegenius.com\/cplusplus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/kindsonthegenius.com\/cplusplus\/#\/schema\/person\/59a11a9f72bb132f56f91ab9f8a58a0e","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b9d710de456c3d85e5614c3a6992fa3d527425e2ab32b8bd5d85bfbaa235004b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/kindsonthegenius.com\/cplusplus\/author\/kindsonthegenius-3\/"}]}},"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/235\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}