Overloading - Rybd04/2143-OOP GitHub Wiki

Definition

Overloading

A feature that allows you to define multiple functions or operators with the same name but with different parameters or behaviors.

Explanation

You would use overloading if you wanted to make two animal classes both have a method called makeNoise. When you call the makeNoise method it will call the one that is specific to the object.

Basic Code Example

#include <iostream>
using namespace std;

// Function to add two integers
int add(int a, int b) {
    return a + b;
}

// Function to add two double values
double add(double a, double b) {
    return a + b;
}

int main() {
    cout << add(3, 4) << endl;        // Calls int version
    cout << add(2.5, 3.5) << endl;    // Calls double version
    return 0;
}

Image

image

Additional Resources

https://github.com/user-attachments/assets/fe55724e-79e2-429c-8ff8-11be2bda843a https://www.w3schools.com/cpp/cpp_function_overloading.asp

⚠️ **GitHub.com Fallback** ⚠️