VOOZH about

URL: https://www.geeksforgeeks.org/cpp/method-overloading-in-cpp-classes/

⇱ Method Overloading in C++ Classes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Method Overloading in C++ Classes

Last Updated : 7 Aug, 2025

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


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:

Constructor Overloading

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

Example:


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.
Comment