Open In App

Method Overloading in C++ Classes

Last Updated : 16 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, method overloading refers to defining multiple functions within the same class with the same name but different parameter lists. It allows a class to provide different behaviours for a method depending on the arguments passed to it. This is a form of compile-time polymorphism.

Method overloading allows a class to have more than one function with the same name but differing in:

  • Number of parameters
  • Type of parameters
  • Order of parameters

Example

C++
#include <iostream>
using namespace std;

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

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

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

int main() {
    Calculator calc;

    cout << "Sum of 2 + 3: "
        << calc.add(2, 3) << endl;
    cout << "Sum of 1 + 2 + 3: "
        << calc.add(1, 2, 3) << endl;
    cout << "Sum of 2.5 + 3.5: "
        << calc.add(2.5, 3.5);

    return 0;
}

Output
Sum of 2 + 3: 5
Sum of 1 + 2 + 3: 6
Sum of 2.5 + 3.5: 6

How Method Overloading it works?

The compiler determines at compile time which method to call based on:

  • Number of arguments
  • Types of arguments
  • Order of arguments

For example:

calc.add(2, 3) calls add(int, int)

calc.add(2.5, 3.5) calls add(double, double)

Important Points

Keep in mind that return type alone cannot differentiate overloaded methods. It is because if you call the function without specifying the type of variable that stores it return value (which is a valid function calling), then there is no way compiler can determine which function is called.

Also, default arguments in overloaded methods can cause ambiguity:

C++
void display(int a = 0);

// Ambiguous — both match a call like display();
void display();

Constructor Overloading

Just like normal functions, parameterized constructors can also be overloaded on the basis of its parameters.

Example:

C++
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;

public:
    // Constructor with two parameters
    Rectangle(int w, int h) {
        width = w;
        height = h;
        cout << "Rectangle created with width = "
             << width << " and height = "
             << height << endl;
    }

    // Constructor with one parameter (square)
    Rectangle(int side) {
        width = side;
        height = side;
        cout << "Square created with side = "
             << side << endl;
    }

    // Constructor with no parameters
    Rectangle() {
        width = 0;
        height = 0;
        cout << "Default rectangle created "
             << "(width = 0, height = 0)" << endl;
    }

    int area() {
        return width * height;
    }
};

int main() {
    // Calls constructor with two parameters
    Rectangle rect1(10, 20);

    // Calls constructor with one parameter (square)
    Rectangle rect2(15);

    // Calls constructor with no parameters
    Rectangle rect3;

    cout << "Area of rect1: " << rect1.area() << endl;
    cout << "Area of rect2: " << rect2.area() << endl;
    cout << "Area of rect3: " << rect3.area();

    return 0;
}

Output
Rectangle created with width = 10 and height = 20
Square created with side = 15
Default rectangle created (width = 0, height = 0)
Area of rect1: 200
Area of rect2: 225
Area of rect3: 0

Advantages of Method Overloading

Method overloading have following main advantages:

  • Simplifies interface design.
  • Enhances readability and maintainability.
  • Allows code reuse with varying inputs.

Next Article
Practice Tags :

Similar Reads