Skip to content
Related Articles

Related Articles

Improve Article
Function Overloading in C++
  • Difficulty Level : Easy
  • Last Updated : 17 Jun, 2021

Function overloading is a feature of object oriented programming where two or more functions can have the same name but different parameters.

When a function name is overloaded with different jobs it is called Function Overloading.

In Function Overloading “Function” name should be the same and the arguments should be different.

Function overloading can be considered as an example of polymorphism feature in C++.

Following is a simple C++ example to demonstrate function overloading.



CPP




#include <iostream>
using namespace std;
 
void print(int i) {
  cout << " Here is int " << i << endl;
}
void print(double  f) {
  cout << " Here is float " << f << endl;
}
void print(char const *c) {
  cout << " Here is char* " << c << endl;
}
 
int main() {
  print(10);
  print(10.10);
  print("ten");
  return 0;
}

Output:

Here is int 10 
Here is float 10.1 
Here is char* ten

How  Function Overloading works?

  • Exact match:- (Function name and Parameter)
  • If a not exact match is found:

               ->Char, Unsigned char, and short are promoted to an int.

               ->Float is promoted to double

  • If no match found:

               ->C++ tries to find a match through the standard conversion.

  • ELSE ERROR 🙁
  1. Function overloading and return type
  2. Functions that cannot be overloaded in C++
  3. Function overloading and const keyword
  4. Function Overloading vs Function Overriding in C++

Recent articles on function overloading in C++
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :