{"id":233,"date":"2020-09-01T15:51:34","date_gmt":"2020-09-01T15:51:34","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=233"},"modified":"2020-09-01T15:51:34","modified_gmt":"2020-09-01T15:51:34","slug":"c-overloading","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-overloading\/","title":{"rendered":"C++ Overloading"},"content":{"rendered":"<p>In this lesson we would cover C++ overloading. Precisely, you&#8217;ll learn about <strong><em>Function Overloading<\/em><\/strong> and <strong><em>Operator Overloading<\/em><\/strong>.<\/p>\n<ol>\n<li><a href=\"#t1\">Function Overloading<\/a><\/li>\n<li><a href=\"#t2\">Operator Overloading<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Function Overloading<\/strong><\/h4>\n<p>In function overloading, two functions have the same name and exist in the same scope. However, both functions have they must have different arguments specification. In this case, we say that they are overloaded functions.<\/p>\n<p>For example, all these are overloaded functions<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Example of Overloaded functions<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getValue<\/span>() { }\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getValue<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> x) { }\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">float<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getValue<\/span>(<span style=\"color: #333399; font-weight: bold;\">double<\/span> x) { }\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getValue<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> x, <span style=\"color: #333399; font-weight: bold;\">double<\/span> y) { }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>About return type: notice from the examples that not all overloaded functions have the same return time. The key is this: overloaded functions must have different argument. This is regardless of whether they have different return types or not.<\/p>\n<p>So the code below would result in error.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Error: different return types, but same parameters<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getSum<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> a, <span style=\"color: #333399; font-weight: bold;\">int<\/span> b) { }\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">float<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getSum<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> a, <span style=\"color: #333399; font-weight: bold;\">int<\/span> b) { }\r\n<\/pre>\n<p>Let&#8217;s write a program to demonstrate how function overloading works.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Demonstrating 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: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTotal<\/span>(<span style=\"color: #333399; font-weight: bold;\">int<\/span> x, <span style=\"color: #333399; font-weight: bold;\">int<\/span> y, <span style=\"color: #333399; font-weight: bold;\">int<\/span> z) {\r\n  <span style=\"color: #008800; font-weight: bold;\">return<\/span> x <span style=\"color: #333333;\">+<\/span> y <span style=\"color: #333333;\">+<\/span> z;\r\n}\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">double<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getTotal<\/span>(<span style=\"color: #333399; font-weight: bold;\">double<\/span> x, <span style=\"color: #333399; font-weight: bold;\">double<\/span> y, <span style=\"color: #333399; font-weight: bold;\">int<\/span> z) {\r\n  <span style=\"color: #008800; font-weight: bold;\">return<\/span> x <span style=\"color: #333333;\">+<\/span> y;\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  <span style=\"color: #333399; font-weight: bold;\">int<\/span> total1 <span style=\"color: #333333;\">=<\/span> getTotal(<span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>);\r\n\r\n  <span style=\"color: #333399; font-weight: bold;\">double<\/span> total2 <span style=\"color: #333333;\">=<\/span> getTotal(<span style=\"color: #6600ee; font-weight: bold;\">4.3<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">7.25<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>);\r\n\r\n  cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"First total: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> total1 <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n  cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Second total: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> total2;\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:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">First total: 10\r\nSecond total: 11.55\r\n<\/pre>\n<p>This code is quite clear, so I&#8217;m not going to go over it. However, I recommend you run it to understand how it works<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Operator Overloading<\/strong><\/h4>\n<p>Basically, the concept of overloading is about &#8216;one thing being able to perform different operations&#8217;. For example, we can make existing operator perform different action.<\/p>\n<p>To overload an operator, we use the keyword &#8216;operator&#8217; followed by the operator we want to overload. Then in curly braces, we define the operation that need to be done. In the code below, we overload the + operator to add two Square objects.<\/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: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Square<\/span> {\r\n   <span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">double<\/span> getArea(<span style=\"color: #333399; font-weight: bold;\">void<\/span>) {\r\n         <span style=\"color: #008800; font-weight: bold;\">return<\/span> length <span style=\"color: #333333;\">*<\/span> height;\r\n      }\r\n\r\n      <span style=\"color: #333399; font-weight: bold;\">void<\/span> setLength( <span style=\"color: #333399; font-weight: bold;\">double<\/span> len ) {\r\n         length <span style=\"color: #333333;\">=<\/span> len;\r\n      }\r\n\r\n      <span style=\"color: #333399; font-weight: bold;\">void<\/span> setHeight( <span style=\"color: #333399; font-weight: bold;\">double<\/span> hei ) {\r\n         height <span style=\"color: #333333;\">=<\/span> hei;\r\n      }\r\n\r\n      <span style=\"color: #888888;\">\/\/ Overload + operator to add two Square objects.<\/span>\r\n      Square <span style=\"color: #008800; font-weight: bold;\">operator<\/span><span style=\"color: #333333;\">+<\/span>(<span style=\"color: #008800; font-weight: bold;\">const<\/span> Square<span style=\"color: #333333;\">&amp;<\/span> s) {\r\n    \t Square square;\r\n    \t square.length <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">-&gt;<\/span>length <span style=\"color: #333333;\">+<\/span> s.length;\r\n    \t square.height <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">-&gt;<\/span>height <span style=\"color: #333333;\">+<\/span> s.height;\r\n         <span style=\"color: #008800; font-weight: bold;\">return<\/span> square;\r\n      }\r\n\r\n   <span style=\"color: #997700; font-weight: bold;\">private:<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">double<\/span> length;      <span style=\"color: #888888;\">\/\/ Length of a square<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">double<\/span> height;      <span style=\"color: #888888;\">\/\/ Height of a square<\/span>\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   Square square1;    <span style=\"color: #888888;\">\/\/ Declare square1<\/span>\r\n   Square square2;    <span style=\"color: #888888;\">\/\/ Declare square2<\/span>\r\n   Square square3;    <span style=\"color: #888888;\">\/\/ Declare square3<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">double<\/span> area <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">0.0<\/span>; <span style=\"color: #888888;\">\/\/ Store area of a square<\/span>\r\n\r\n   <span style=\"color: #888888;\">\/\/ square 1 specification<\/span>\r\n   square1.setLength(<span style=\"color: #6600ee; font-weight: bold;\">9.0<\/span>);\r\n   square1.setHeight(<span style=\"color: #6600ee; font-weight: bold;\">12.0<\/span>);\r\n\r\n   <span style=\"color: #888888;\">\/\/ square 2 specification<\/span>\r\n   square2.setLength(<span style=\"color: #6600ee; font-weight: bold;\">11.0<\/span>);\r\n   square2.setHeight(<span style=\"color: #6600ee; font-weight: bold;\">6.0<\/span>);\r\n\r\n   <span style=\"color: #888888;\">\/\/ area of square 1<\/span>\r\n   area <span style=\"color: #333333;\">=<\/span> square1.getArea();\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Area of sueare1 : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> area <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n   <span style=\"color: #888888;\">\/\/ area of square 2<\/span>\r\n   area <span style=\"color: #333333;\">=<\/span> square2.getArea();\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Area of square2 : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> area <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n   <span style=\"color: #888888;\">\/\/ Add two square as follows:<\/span>\r\n   square3 <span style=\"color: #333333;\">=<\/span> square1 <span style=\"color: #333333;\">+<\/span> square2;\r\n\r\n   <span style=\"color: #888888;\">\/\/ area of square 3<\/span>\r\n   area <span style=\"color: #333333;\">=<\/span> square3.getArea();\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Area of square3 : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> area <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\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 program yields the following output:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Area of sueare1 <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">108<\/span>\r\nArea of square2 <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">66<\/span>\r\nArea of square3 <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">360<\/span>\r\n<\/pre>\n<p>From the program above, we have overloaded the + operator to be able to add two Squares. The operation produces a third square whose length is the sum of the two given squares. Similarly, the height is the sum of the two squares being summed.<\/p>\n<p>However, the following operators cannot be overloaded:<\/p>\n<ul>\n<li>scope resolution(::)<\/li>\n<li>pointer (*)<\/li>\n<li>dot (.)<\/li>\n<li>ternary (?:)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson we would cover C++ overloading. Precisely, you&#8217;ll learn about Function Overloading and Operator Overloading. Function Overloading Operator Overloading &nbsp; 1. Function Overloading &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":[40],"class_list":["post-233","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-overloading"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/233","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=233"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":234,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/233\/revisions\/234"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}