What is overloading in C++?

Overloading in C++ refers to the ability to define multiple functions or operators with the same name but with different parameters. This feature allows for greater flexibility and readability in the code by letting you use the same name for functions that perform similar tasks but operate on different types or numbers of inputs.

Types of Overloading in C++:

1. Function Overloading

Function overloading allows multiple functions to have the same name as long as their parameter lists differ in type, number, or order of parameters. The compiler determines which function to call based on the arguments passed.

Example:

#include <iostream> using namespace std; class Overload { public: // Function to add two integers int add(int a, int b) { return a + b; } // Function to add three integers int add(int a, int b, int c) { return a + b + c; } // Function to add two double values double add(double a, double b) { return a + b; } }; int main() { Overload obj; cout << obj.add(5, 10) << endl; // Calls add(int, int) cout << obj.add(5, 10, 15) << endl; // Calls add(int, int, int) cout << obj.add(5.5, 10.5) << endl; // Calls add(double, double) return 0; }

In this example, the add function is overloaded with different parameter lists.

2. Operator Overloading

Operator overloading allows you to define how operators behave with user-defined types (classes). This enables objects of classes to be manipulated using standard operators like +, -, *, etc.

Example:

#include <iostream> using namespace std; class Complex { public: float real; float imag; Complex(float r, float i) : real(r), imag(i) {} // Overloading the + operator Complex operator + (const Complex& obj) { return Complex(real + obj.real, imag + obj.imag); } }; int main() { Complex c1(1.5, 2.5); Complex c2(2.0, 3.0); Complex c3 = c1 + c2; // Uses overloaded + operator cout << "Result: " << c3.real << " + " << c3.imag << "i" << endl; // Outputs: Result: 3.5 + 5i return 0; }

Here, the + operator is overloaded to allow addition of Complex objects.

Benefits of Overloading:

  • Code Clarity: Overloading improves code readability and maintains consistency, allowing the same operation to be expressed in different contexts.
  • Ease of Use: Functions that perform similar tasks can share the same name, making the API simpler for users.

Conclusion

Overloading is a powerful feature in C++ that enhances the expressiveness and usability of the language. It allows programmers to define multiple functions or operators that perform similar tasks but with different types or numbers of arguments.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Which language is best for multithreading?
What coding language does Google hire?
What Is the Palantir Interview Process Like? (Round by Round)
Palantir's loop is built from a menu of distinctive rounds: Decomposition, Learning, Re-engineering, coding, and design, with culture screened aggressively from the first call.
Which month has the most layoffs?
Why should I work for Meta?
What to say in a technical interview?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.