{"id":230,"date":"2020-09-01T13:23:41","date_gmt":"2020-09-01T13:23:41","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=230"},"modified":"2020-09-01T13:23:41","modified_gmt":"2020-09-01T13:23:41","slug":"c-oop-function-overriding","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-oop-function-overriding\/","title":{"rendered":"C++ OOP &#8211; Function Overriding"},"content":{"rendered":"<p>Overriding is yet another importance feature in OOP. Although its application is not restricted to OOP, we&#8217;ll use OOP as a foundation.<\/p>\n<ol>\n<li><a href=\"#t1\">Overview of Function Overriding<\/a><\/li>\n<li><a href=\"#t2\">Accessing\u00a0 Overridden Functions<\/a><\/li>\n<li><a href=\"#t3\">Using a Pointer to Access Overridden Functions<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Overview of Function Overriding<\/strong><\/h4>\n<p>A you already know, <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-oop-inheritance\/\" target=\"_blank\" rel=\"noopener noreferrer\">inheritance<\/a> allows you to create a class by deriving an existing(base) class. The derived class then inherits members of the base class.<\/p>\n<p>Assuming we have the same member function(with same name) defined in both the derived class and the base class. Now if we call the function from an object of the derived class, then the function in the derived class is executed. Not the parent.<\/p>\n<p>This is what is called <strong>function overriding<\/strong>. In this case, the function in the derived class overrides the same function in the base class.<\/p>\n<p>Let&#8217;s illustrate using an example:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demonstrate function Overriding<\/span>\r\n<span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n\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;\">\/\/ declare a class, shape<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span>  <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span> {\r\n\r\n  <span style=\"color: #997700; font-weight: bold;\">protected:<\/span>\r\n       <span style=\"color: #333399; font-weight: bold;\">double<\/span> width <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">9.2<\/span>;\r\n\r\n       <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">showWidth<\/span>(){\r\n    \t   cout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Shape's width is: \"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span> width;\r\n       }\r\n};\r\n\r\n<span style=\"color: #888888;\">\/\/ create a derived class Circle<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Circle<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> Shape {\r\n\r\n\t<span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n\t\t<span style=\"color: #888888;\">\/\/ Overrides showWidth in Shape<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">void<\/span> showWidth(){\r\n\t\t\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Circle's width is \"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span> width;\r\n\t\t}\r\n};\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\r\n\tCircle circle1;\r\n\r\n\tcircle1.showWidth();\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><strong>Output<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Circle's width is 9.2\r\n<\/pre>\n<p>In the above code, the <em><strong>showWidth()<\/strong><\/em> function in the child overrides the <strong><em>showWidth()<\/em><\/strong> in the base class. Therefore, what executes is the child&#8217;s function.<\/p>\n<p>Also note the the width used in the child comes from the base. It&#8217;s a protected data member and therefore available to derived class.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Accessing Overridden Functions<\/strong><\/h4>\n<p>A function that has been overridden in a derived class can still be accessed. You can do this using the scope resolution operator(::).<\/p>\n<p>The program below shows this:<\/p>\n<p>&lt;!&#8211; HTML generated using hilite.me &#8212;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Accessing an Overridden Method<\/span>\r\n<span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n\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;\">\/\/ declare a class, shape<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span>  <span style=\"color: #bb0066; font-weight: bold;\">Shape<\/span> {\r\n\r\n  <span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n       <span style=\"color: #333399; font-weight: bold;\">double<\/span> width <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">9.2<\/span>;\r\n\r\n       <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">showWidth<\/span>(){\r\n    \t   cout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"(base)Shape's width is: \"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span> width;\r\n       }\r\n};\r\n\r\n<span style=\"color: #888888;\">\/\/ create a derived class Circle<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Circle<\/span><span style=\"color: #333333;\">:<\/span> <span style=\"color: #008800; font-weight: bold;\">public<\/span> Shape {\r\n\r\n\t<span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n\t\t<span style=\"color: #888888;\">\/\/ Overrides showWidth in Shape<\/span>\r\n\t\t<span style=\"color: #333399; font-weight: bold;\">void<\/span> showWidth(){\r\n\t\t\tcout<span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"(derived)Circle's width is \"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span> width <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\t\t}\r\n};\r\n\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\r\n\tCircle circle1;\r\n\r\n\tcircle1.showWidth();\r\n\r\n\tcircle1.Shape<span style=\"color: #333333;\">::<\/span>showWidth();\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 is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">(derived)Circle's width is 9.2\r\n(base)Shape's width is: 9.2\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Calling an Overridden Function Using a Pointer.<\/strong><\/h4>\n<p>In this case, we use a pointer of the base class to point to an object of the derived class. Then we call the function from that pointer.<\/p>\n<p>I show only the main method here.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\r\nCircle circle1;\r\n\r\n<span style=\"color: #888888;\">\/\/pointer of the base class pointing to circle1<\/span>\r\nShape<span style=\"color: #333333;\">*<\/span> shapePointer <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">&amp;<\/span>circle1;\r\n\r\n<span style=\"color: #888888;\">\/\/call the function of the base class using shapePointer<\/span>\r\nshapePointer<span style=\"color: #333333;\">-&gt;<\/span>showWidth();\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>Output<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">(base)Shape's width is: 9.2\r\n<\/pre>\n<p>So here, we first created an object of the derived class, circle1. Then we created a pointer, shapePointer. This pointer holds the address of the derived class. But we can then use it to call the showWidth() function, it calls the function from the base class.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overriding is yet another importance feature in OOP. Although its application is not restricted to OOP, we&#8217;ll use OOP as a foundation. Overview of Function &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],"tags":[39],"class_list":["post-230","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-function-overriding"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/230","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=230"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/230\/revisions"}],"predecessor-version":[{"id":232,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/230\/revisions\/232"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}