By end of this tutorial, you will be able to learn more about the function overloading, what is function overloading and how to use function overloading in c++ programming language. I have defined the function overloading with the help of an example written in c++ programs.
When you used the same function with different parameters, then it called the function overloading. Below is the example of function overloading. In the below example I have define int display()
function with the different parameters.
1 2 3 4 | int display() { } int display(int a) { } float display(double a) { } int display(int a, double b) { } |
You can use the function overloading in C++ programs by defining the same function with different parameters for different purposes. See the below example for more understanding the function overloading.
In the below example, we have defined the void display()
function for different purposes with the different parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> using namespace std; void display(int); void display(float); void display(int, float); int main() { int a = 5; float b = 5.5; display(a); display(b); display(a, b); return 0; } void display(int var) { cout << "Integer number: " << var << endl; } void display(float var) { cout << "Float number: " << var << endl; } void display(int var1, float var2) { cout << "Integer number: " << var1; cout << " and float number:" << var2; } |
Integer number: 5
Float number: 5.5
Integer number: 5 and float number: 5.5
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c++ function overloading, example of function overloading in c++, function overloading, overloaded function in c++ programs