Overloading - EduardoMSU/OOP-2143 GitHub Wiki

Overloading in Object-Oriented Programming (OOP)

What is Overloading?

Overloading in object-oriented programming refers to the ability to define multiple methods or functions with the same name but different parameter lists (either by type or number of parameters). Overloading allows different behaviors depending on the parameters passed to the method or function.

Overloading is typically applied to:

  • Method Overloading: Defining multiple methods with the same name but different parameters in the same class.
  • Operator Overloading: Defining custom behavior for operators when they are used with user-defined types.

Key Characteristics of Overloading

Characteristic Description
Same Method Name Overloaded methods must have the same name but differ in the number or type of parameters.
Different Parameter List Overloading is determined by the number or type of arguments passed to the method.
No Return Type Consideration Return type does not play a role in method overloading; only the method signature (name and parameters) matters.
Compile-Time Resolution Overloading is resolved at compile-time based on the method's signature.

Method Overloading Example in C++

Example of Overloading Methods

#include <iostream>
using namespace std;

class Calculator {
public:
    // Overloaded method to add two integers
    int add(int a, int b) {
        return a + b;
    }

    // Overloaded method to add three integers
    int add(int a, int b, int c) {
        return a + b + c;
    }

    // Overloaded method to add two doubles
    double add(double a, double b) {
        return a + b;
    }
};

int main() {
    Calculator calc;

    cout << "Sum of 5 and 10: " << calc.add(5, 10) << endl;
    cout << "Sum of 5, 10, and 15: " << calc.add(5, 10, 15) << endl;
    cout << "Sum of 5.5 and 10.5: " << calc.add(5.5, 10.5) << endl;

    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️