What is overloading in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
Design Gurus Team

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
What is NOT LIKE operator in SQL?
5 reasons why a FAANG company may not be right for you.
What is the behavioral interview format?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.