{"id":81415,"date":"2021-07-10T09:00:45","date_gmt":"2021-07-10T03:30:45","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81415"},"modified":"2021-07-10T09:00:45","modified_gmt":"2021-07-10T03:30:45","slug":"function-overloading-and-overriding-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/","title":{"rendered":"Function Overloading in C++ | Function Overriding in C++"},"content":{"rendered":"<p>In this article, we will learn about function overloading and function overriding in C++. We will also discuss the differences in both. Let&#8217;s start!!!<\/p>\n<h3>Function Overloading in C++<\/h3>\n<p>In C++, two or more functions can have the same name if the number and\/or type of parameters are different, this is called function overloading. Thus, overloaded functions are functions that have the same name but different parameters.<\/p>\n<p>However, if in two or more functions, only return type is different keeping number and type of parameters same, it is not considered function overloading. In this case, the compiler gives an error.<\/p>\n<p>Function overloading is achieved during compile time. It is a form of compile time polymorphism.<\/p>\n<p><strong>Examples of overloaded functions<\/strong><\/p>\n<p>int add(int, int);<br \/>\nint add(int, int, int); \/\/number of parameters different<br \/>\ndouble add(double, double); \/\/type of parameters different<\/p>\n<p><strong>Example to illustrate function overloading in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint add(int a, int b) {\n    return a+b;\n}\n\nint add(int a, int b, int c) {\n    return a+b+c;\n}\n\ndouble add(double a, double b) {\n    return a+b;\n}\n\nint main() {\n  int x = 3, y = 7, z = 12;\n  double n1 = 4.56, n2 = 13.479;\n  \n  cout&lt;&lt;\"x+y = \"&lt;&lt;add(x,y)&lt;&lt;endl;\n  cout&lt;&lt;\"x+y+z = \"&lt;&lt;add(x,y,z)&lt;&lt;endl;\n  cout&lt;&lt;\"n1+n2 = \"&lt;&lt;add(n1,n2);\n  \n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">x+y = 10<br \/>\nx+y+z = 22<br \/>\nn1+n2 = 18.039<\/div>\n<h3>Function Overriding in C++<\/h3>\n<p>When a member function of a base class is redefined in its derived class with the same parameters and return type, it is called function overriding in C++. Now, if we use an object of the derived class to call this function, the function defined in the derived class is invoked. The base class function is said to be overridden.<\/p>\n<p>Function overriding is achieved during runtime. It is a form of runtime polymorphism.<\/p>\n<p><strong>Example to illustrate function overriding in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nclass base {\n    public:\n    virtual void display() {\n        cout&lt;&lt;\"Function of base class\"&lt;&lt;endl;\n    }\n};\nclass derived : public base {\n    public:\n    void display() {\n        cout&lt;&lt;\"Function of derived class\"&lt;&lt;endl;\n    }\n};\n\nint main() {\n  derived d1;\n  d1.display();\n  return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Function of derived class<\/div>\n<p>We use virtual keyword with the base class function to override it rather than access it.<\/p>\n<h3>Difference between Function Overloading and Function Overriding<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Basis<\/b><\/td>\n<td><b>Function Overloading<\/b><\/td>\n<td><b>Function Overriding<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Inheritance<\/span><\/td>\n<td><span style=\"font-weight: 400\">Can happen without inheritance<\/span><\/td>\n<td><span style=\"font-weight: 400\">Happens only when a class inherits from another class<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Signature<\/span><\/td>\n<td><span style=\"font-weight: 400\">Function signatures of overloaded functions must be different\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Same function signatures<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Polymorphism<\/span><\/td>\n<td><span style=\"font-weight: 400\">Compile time\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Runtime<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Scope<\/span><\/td>\n<td><span style=\"font-weight: 400\">Overloaded functions are in the same scope<\/span><\/td>\n<td><span style=\"font-weight: 400\">Functions are in different scope<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Purpose<\/span><\/td>\n<td><span style=\"font-weight: 400\">To have multiple functions with same name that act differently depending on parameters<\/span><\/td>\n<td><span style=\"font-weight: 400\">To perform additional or different tasks than the base class function<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Number of times<\/span><\/td>\n<td><span style=\"font-weight: 400\">A function can be overloaded multiple times<\/span><\/td>\n<td><span style=\"font-weight: 400\">A function is overridden single time in its derived class<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Summary<\/h3>\n<p>This article covers function overloading and function overriding in C++. We learnt how to implement these using examples. Then, we listed down the differences between function overloading and function overriding.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn about function overloading and function overriding in C++. We will also discuss the differences in both. Let&#8217;s start!!! Function Overloading in C++ In C++, two or more functions&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81752,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3707,3708,3709],"class_list":["post-81415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-function-overloading-in-c","tag-function-overloading-vs-overriding-in-c","tag-function-overriding-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Function Overloading in C++ | Function Overriding in C++ - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about function overloading and function overriding in C++. Check the differences between functions overloading and overriding in C++.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Function Overloading in C++ | Function Overriding in C++ - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about function overloading and function overriding in C++. Check the differences between functions overloading and overriding in C++.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-10T03:30:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Overloading-and-Overriding-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Function Overloading in C++ | Function Overriding in C++ - TechVidvan","description":"Learn about function overloading and function overriding in C++. Check the differences between functions overloading and overriding in C++.","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:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Function Overloading in C++ | Function Overriding in C++ - TechVidvan","og_description":"Learn about function overloading and function overriding in C++. Check the differences between functions overloading and overriding in C++.","og_url":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-10T03:30:45+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Overloading-and-Overriding-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Function Overloading in C++ | Function Overriding in C++","datePublished":"2021-07-10T03:30:45+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/"},"wordCount":400,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Overloading-and-Overriding-in-C.jpg","keywords":["function overloading in c++","Function overloading vs overriding in C++","Function overriding in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/","name":"Function Overloading in C++ | Function Overriding in C++ - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Overloading-and-Overriding-in-C.jpg","datePublished":"2021-07-10T03:30:45+00:00","description":"Learn about function overloading and function overriding in C++. Check the differences between functions overloading and overriding in C++.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Overloading-and-Overriding-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Overloading-and-Overriding-in-C.jpg","width":1200,"height":628,"caption":"Overloading and Overriding in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/function-overloading-and-overriding-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Function Overloading in C++ | Function Overriding in C++"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81415"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81415\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81752"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}