In C++ language, we can create more than one function with the same name but each function must have a different parameter list.
Such functions are called overloaded functions and this process is known as function overloading.
Function overloading is an example of polymorphism.
Polymorphism means one function has many forms.
Example of Function Overloading in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; void show(); void show(int); void show(char); int main() { show(); show(10); show('a'); } void show() // overloaded function { cout << "inside show"<<endl; } void show(int a) // overloaded function { cout << "a = " << a<<endl; } void show(char c) // overloaded function { cout << "character = " << c<<endl; } |
Output
1 2 3 | inside show a = 10 character = a |
Description: In the above program, we have created three function with the same name “show” but each show() function have a different parameter list
Q: Write a C++ program to calculate the area of a rectangle, triangle, and circle using function overloading.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; void area(double); void area(int, int); void area(double, double); int main() { area(5.5); area(10, 20); area(10.1, 11.2); } void area(double r) // overloaded function { cout << "\n Area of circle =" << (3.14 * r * r); } void area(int l, int b) // overloaded function { cout << "\n Area of rectangle =" << (l * b); } void area(double b, double h) // overloaded function { cout << "\n Area of triangle =" << ((0.5) * b * h); } |
Output
1 2 3 | Area of circle =94.985 Area of rectangle =200 Area of triangle =56.56 |
Function Overloading in Classes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; class example { public: void show() // overloaded function { cout << " Inside show"; } void show(int a) // overloaded function { cout << "\n a=" << a; } void show(char c) // overloaded function { cout << "\n character =" << c; } }; int main() { example obj; obj.show(); obj.show(10); obj.show('z'); } |
Output
1 2 3 | Inside show a=10 character =z |
Q: Write a C++ program to Overload add method in a class
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> class MathOperations { public: int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }; int main() { MathOperations math; int intSum = math.add(5, 10); std::cout << "Sum of integers: " << intSum << std::endl; double doubleSum = math.add(3.5, 2.7); std::cout << "Sum of doubles: " << doubleSum << std::endl; int tripleSum = math.add(2, 4, 6); std::cout << "Sum of three integers: " << tripleSum << std::endl; return 0; } |
When to Use Function Overloading
Function overloading is ideal for scenarios where a function performs similar actions on different data types. It’s commonly used in mathematical calculations, input/output operations, and conversions.
Implementing Function Overloading in Real Projects
Consider a scenario where a geometry library needs to compute the area of various shapes. By overloading a single “calculateArea” function, the library can handle different shapes effortlessly.
Best Practices for Effective Use
- Keep function names meaningful and descriptive.
- Use consistent parameter names for overloaded functions.
- Avoid excessive overloading, as it can confuse developers.
Read More
Local and Nested Classes in C++
Inheritance and types of Inheritance in C++
FAQs (Frequently Asked Questions)
No, function overloading is based on the function’s name and parameter list, but not on return types.
No, function overloading can be applied to both member functions and standalone functions.
If the compiler cannot determine a single best match, it will result in a compilation error due to ambiguity.
Yes, you can overload functions with different access specifiers. The access specifier is not considered when determining which function to call.