techmore.in

C++ Overloading

In C++, overloading allows multiple functions or operators to share the same name but perform different tasks depending on their parameter types or the number of parameters. There are two types of overloading:

  1. Function Overloading: Creating multiple functions with the same name but different signatures (parameter types or numbers).
  2. Operator Overloading: Defining new behavior for existing operators when applied to objects of a class.

1. Function Overloading

Function overloading lets you define multiple functions with the same name but varying argument lists. C++ determines which function to call based on the arguments passed.

Example:

cpp
#include using namespace std; class Math { public: // Function to add two integers int add(int a, int b) { return a + b; } // Overloaded function to add two floats float add(float a, float b) { return a + b; } // Overloaded function to add three integers int add(int a, int b, int c) { return a + b + c; } }; int main() { Math math; cout << "Sum of two integers: " << math.add(10, 20) << endl; // Calls int add(int, int) cout << "Sum of two floats: " << math.add(3.5f, 4.5f) << endl; // Calls float add(float, float) cout << "Sum of three integers: " << math.add(10, 20, 30) << endl; // Calls int add(int, int, int) return 0; }

Key Points

  • The return type of overloaded functions doesn’t need to be the same.
  • Overloading is determined by the number and types of parameters.

2. Operator Overloading

In C++, operators like +, -, *, and == can be overloaded to work with user-defined types (such as classes). You can define how operators behave when applied to class objects.

Syntax for Operator Overloading:

cpp
class ClassName { public: returnType operator symbol(parameters) { // Implementation } };

Example of Overloading the + Operator:

cpp
#include using namespace std; class Complex { private: float real; float imag; public: // Constructor to initialize complex number Complex(float r = 0, float i = 0) : real(r), imag(i) {} // Overloading the + operator to add two complex numbers Complex operator + (const Complex& other) { Complex temp; temp.real = real + other.real; temp.imag = imag + other.imag; return temp; } // Function to display the complex number void display() { cout << real << " + " << imag << "i" << endl; } }; int main() { Complex c1(3.5, 2.5); Complex c2(1.5, 3.0); Complex c3 = c1 + c2; // Calls the overloaded + operator c3.display(); // Output: 5 + 5.5i return 0; }

Explanation:

  • : This is the function that overloads the + operator for the Complex class. It allows the addition of two Complex objects.
  • You can define similar overloads for other operators (-, *, ==, etc.).

Rules of Operator Overloading:

  1. You can’t overload the following operators: ::, .:, sizeof, ?:, and typeid.
  2. The overloaded operator must have at least one user-defined type (class or struct).
  3. You can’t change the precedence or associativity of operators.

Overloading the == Operator:

Here’s an example of overloading the == operator to compare two complex numbers:

cpp
class Complex { public: float real; float imag; Complex(float r = 0, float i = 0) : real(r), imag(i) {} // Overload the == operator bool operator == (const Complex& other) { return (real == other.real && imag == other.imag); } }; int main() { Complex c1(3.0, 2.0), c2(3.0, 2.0); if (c1 == c2) { cout << "Complex numbers are equal." << endl; } else { cout << "Complex numbers are not equal." << endl; } return 0; }

Summary

  • Function Overloading allows multiple functions with the same name but different parameter lists.
  • Operator Overloading allows existing C++ operators to be redefined for user-defined types.