GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Function Overloading in C++
Selected Reading
Function Overloading in C++Function overloading in C++ allows you to define multiple functions with the same name but different parameters. Function overloading is used to achieve polymorphism which is an important concept of object-oriented programming systems. Syntax for Overloaded FunctionsConsider the following two function declarations having the same name but different parameters − return_type function_name(parameter1); return_type function_name(parameter2); Example of Function OverloadingIn the following example, we are defining three different functions with the same name but different parameters. This example demonstrates the implementation of function overloading − #include<iostream>
using namespace std;
// Adding two integers (Function definition 1)
int addition(int a, int b) {
return a + b;
}
// Adding three integers (Function definition 2)
int addition(int a, int b, int c) {
return a + b + c;
}
// Adding two floating-point numbers (Function definition 3)
float addition(float a, float b) {
return a + b;
}
int main() {
cout<<addition(10.5f, 20.3f)<<endl;
cout<<addition(10, 20, 30)<<endl;
cout<<addition(10, 20)<<endl;
return 0;
}
Output30.8 60 30 How Function Overloading Works?In the case of different functions with the same name (function overloading), when the compiler reaches a specific function call, it checks with the different function definition based on the parameters type, order, or number of arguments, and executes the matched function definition. ExampleLet suppose, there are 3 different function definitions to add numbers with different parameters − // Adding two integers (Function definition 1)
int addition(int a, int b) {
return a + b;
}
// Adding three integers (Function definition 2)
int addition(int a, int b, int c) {
return a + b + c;
}
// Adding two floating-point numbers (Function definition 3)
float addition(float a, float b) {
return a + b;
}
And, you call the function in the following order − addition(10.5f, 20.3f); // Function call 1 addition(10, 20, 30); // Function call 2 addition(10, 20); // Function call 3 In the above function calls, function definition will be called in the following order −
Function Overloading Based on Number of ParametersThis method involves defining multiple functions with the same name but a different number of parameters. Syntaxvoid display(int a); // takes one parameter void display(int a, double b); // takes two parameters ExampleThe following example demonstrates the function overloading based on the number of parameters − #include <iostream>
using namespace std;
// Function overloads based on the number of parameters
void display(int a) {
cout << "Display with one integer: " << a << endl;
}
void display(int a, double b) {
cout << "Display with an integer and a double: " << a << " and " << b << endl;
}
int main() {
// Calls the first overload
display(10);
// Calls the second overload
display(10, 3.14);
double: 10 and 3.14
return 0;
}
OutputDisplay with one integer: 10 Display with an integer and a double: 10 and 3.14 Function Overloading Based on Different Parameter TypesThis method involves defining multiple functions with the same name but different types of parameters. Syntaxvoid show(int a); // parameter with int type void show(double a); // parameter with double type ExampleThe following example demonstrates the function overloading based on the different parameter types − #include <iostream>
using namespace std;
// Function for integer input
void show(int a) {
cout << "Integer value: " << a << std::endl;
}
// Function for double input
void show(double a) {
cout << "Double value: " << a << std::endl;
}
int main() {
show(10);
show(3.14);
return 0;
}
OutputInteger value: 10 Double value: 3.14 Function Overloading Based on Different Parameter OrderThis method involves defining multiple functions with the same name but different sequences of parameters. Syntax// integer followed by a double,
// can be any datatype(bool, char etc)
void display(int a, double b) {
cout << "Integer and Double: " << a << ", " << b << endl;
}
// double followed by an integer
void display(double a, int b) {
cout << "Double and Integer: " << a << ", " << b << endl;
}
ExampleThe following example demonstrates the function overloading based on the different parameter order − #include <iostream>
using namespace std;
void display(int a, double b) {
cout << "Integer and Double: " << a << ", " << b << endl;
}
void display(double a, int b) {
cout << "Double and Integer: " << a << ", " << b <<endl;
}
int main() {
display(10, 3.14);
display(3.14, 10);
return 0;
}
OutputInteger and Double: 10, 3.14 Double and Integer: 3.14, 10 Use Cases for Function OverloadingFunction overloading in C++ is a powerful feature that allows you to use the same function name to perform different tasks based on different parameter lists. This can lead to more readable and maintainable code. Here are some common scenarios and examples where function overloading is useful −
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |