Operator And Function Overloading in C++ (With Code Example And Difference)

Function Overloading In C++

parameters. This is useful when you want to perform the same type of operation, but with different data types or a different number of parameters. When an overloaded function is called, the compiler selects the appropriate function to call based on the arguments passed in.

Code For Function Overloading

#include <iostream>

using namespace std;

int sum(int a, int b) {
    return a + b;
}

double sum(double a, double b) {
    return a + b;
}

int main() {
    int x = 5, y = 10;
    double a = 5.5, b = 10.5;

    cout << "Sum of ints: " << sum(x, y) << endl;
    cout << "Sum of doubles: " << sum(a, b) << endl;

    return 0;
}

In this example, the function sum is overloaded to accept either int or double parameters. When the sum the function is called with int parameters, the compiler selects the first version of sum to call. When the sum function is called with double parameters, the compiler selects the second version of sum to call.

Working:

Function overloading in C++ allows multiple functions to have the same name but different parameters. When a function is called, the compiler selects the appropriate version of the function to call based on the arguments passed in.

The compiler determines which function to call by matching the number, type, and order of the arguments with the prototypes of the overloaded functions. If there are multiple overloaded functions that match the arguments, the compiler will choose the one that provides the best match based on the type conversion rules.

Operator Overloading In C++

Operator overloading is a feature in C++ that allows operators (e.g. +, -, *, /) to have different meanings for different data types. This allows you to write more natural, expressive code that closely resembles mathematical notation.

When an operator is overloaded, a special operator function is defined for the custom data type. The operator function defines the behavior of the operator when it is used with instances of the custom data type.

Code For Operator Overloading

#include <iostream>

class Complex {
    private:
        double real, imag;
    public:
        Complex(double r, double i) : real(r), imag(i) {}
        Complex operator+(const Complex &other) const {
            return Complex(real + other.real, imag + other.imag);
        }
        void print() const {
            cout << real << " + " << imag << "i" << endl;
        }
};

int main() {
    Complex c1(1, 2), c2(3, 4);
    Complex c3 = c1 + c2;
    c3.print();

    return 0;
}

In the above example, the Complex class is defined with two private data members real and imag. The operator+ function is then defined to overload the + operator for Complex objects. The operator+ function takes a Complex object as an argument and returns a new Complex object that represents the sum of the two Complex objects.

In the main function, two Complex objects are created and the + operator is used to add them together. The result is stored in a third Complex object and is then printed to the console. The output will be 4 + 6i.

Working:

Operator overloading in C++ works by defining special operator functions for a custom data type. These operator functions define the behavior of the operator when it is used with instances of the custom data type.

When an operator is used with instances of the custom data type, the compiler selects the appropriate operator function to call based on the data types of the operands. If a matching operator function has been defined, the compiler will call that function to perform the operation. If no matching operator function has been defined, the compiler will use the default behavior for the operator.

Difference between Operator and Function Overloading

Here’s a pointwise comparison between operator and function overloading:

  1. Definition: Function overloading allows multiple functions with the same name to be defined with different parameters, while operator overloading allows multiple definitions of operator symbols (such as +, -, *, etc.) to be used with custom data types.
  2. Syntax: Function overloading is achieved by declaring multiple functions with the same name but different parameters. Operator overloading is achieved by declaring a special operator function for the operator symbol that needs to be overloaded.
  3. Use case: Function overloading is used to provide multiple ways to perform the same operation based on the input parameters, while operator overloading is used to provide custom behavior for operator symbols when applied to custom data types.
  4. Purpose: The purpose of function overloading is to simplify code and make it more readable, while the purpose of operator overloading is to allow custom behavior for operator symbols when used with custom data types.
  5. Limitations: Function overloading can have limitations on the number of parameters and types of parameters, while operator overloading has limitations on the set of operators that can be overloaded and the number of ways they can be overloaded.
  6. Impact on code: Function overloading can greatly impact the readability and maintainability of code by allowing multiple functions with the same name to perform different operations, while operator overloading can provide a more natural and intuitive way to use operator symbols with custom data types.