Function Overloading in C++

  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++:

#include 
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"<Output

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.

#include 
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

 Area  of circle =94.985
 Area of rectangle =200
 Area of triangle =56.56
 

Function Overloading in Classes

#include 
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

Inside show
 a=10
 character =z
 

Q: Write a C++ program to Overload add method in a class

#include 

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)

Q1: Can I Overload Functions Based On Return Types?

No, function overloading is based on the function's name and parameter list, but not on return types.

Q2: Is Function Overloading Applicable Only To Member Functions?

No, function overloading can be applied to both member functions and standalone functions.

Q3: What Happens If The Compiler Cannot Determine The Best Match During Function Overloading?

If the compiler cannot determine a single best match, it will result in a compilation error due to ambiguity.

Q4: Can I Overload Functions With Different Access Specifiers, Such As Public And Private?

Yes, you can overload functions with different access specifiers. The access specifier is not considered when determining which function to call.

Categories C++