Overloading Unary and Binary Operators in C++


In C++, operator overloading allows developers to redefine how operators work for user-defined types. Unary operators operate on a single operand, while binary operators operate on two operands. Overloading these operators makes custom types more intuitive and easy to use.

Overloading Unary Operators

Unary operators like +, -, ++, and -- can be overloaded to define custom behavior for a single operand. Below is an example of overloading the unary - operator:

    #include <iostream>

    class Point {
    private:
        int x, y;

    public:
        Point(int x = 0, int y = 0) : x(x), y(y) {}

        // Overloading unary - operator
        Point operator-() const {
            return Point(-x, -y);
        }

        void display() const {
            std::cout << "(" << x << ", " << y << ")";
        }
    };

    int main() {
        Point p1(3, 4);
        Point p2 = -p1;

        std::cout << "Original Point: ";
        p1.display();
        std::cout << "\nNegated Point: ";
        p2.display();
        std::cout << std::endl;

        return 0;
    }
        

Output:

    Original Point: (3, 4)
    Negated Point: (-3, -4)
        

In this example, the unary - operator negates the x and y coordinates of the Point.

Overloading Binary Operators

Binary operators like +, -, *, and / can be overloaded to define custom behavior for two operands. Below is an example of overloading the binary + operator:

    #include <iostream>

    class Point {
    private:
        int x, y;

    public:
        Point(int x = 0, int y = 0) : x(x), y(y) {}

        // Overloading binary + operator
        Point operator+(const Point& other) const {
            return Point(x + other.x, y + other.y);
        }

        void display() const {
            std::cout << "(" << x << ", " << y << ")";
        }
    };

    int main() {
        Point p1(1, 2), p2(3, 4);
        Point p3 = p1 + p2;

        std::cout << "Point 1: ";
        p1.display();
        std::cout << "\nPoint 2: ";
        p2.display();
        std::cout << "\nSum: ";
        p3.display();
        std::cout << std::endl;

        return 0;
    }
        

Output:

    Point 1: (1, 2)
    Point 2: (3, 4)
    Sum: (4, 6)
        

Here, the binary + operator adds the respective coordinates of two Point objects.

Overloading Pre-increment and Post-increment Operators

The ++ operator can be overloaded for both pre-increment and post-increment operations. Here is an example:

    #include <iostream>

    class Counter {
    private:
        int value;

    public:
        Counter(int value = 0) : value(value) {}

        // Overloading pre-increment
        Counter& operator++() {
            ++value;
            return *this;
        }

        // Overloading post-increment
        Counter operator++(int) {
            Counter temp = *this;
            ++value;
            return temp;
        }

        void display() const {
            std::cout << value;
        }
    };

    int main() {
        Counter c(5);

        std::cout << "Initial value: ";
        c.display();

        std::cout << "\nAfter pre-increment: ";
        (++c).display();

        std::cout << "\nAfter post-increment: ";
        (c++).display();

        std::cout << "\nFinal value: ";
        c.display();
        std::cout << std::endl;

        return 0;
    }
        

Output:

    Initial value: 5
    After pre-increment: 6
    After post-increment: 6
    Final value: 7
        

In this example, the pre-increment and post-increment behaviors are differentiated by the use of the dummy int parameter for post-increment.

Conclusion

Overloading unary and binary operators in C++ allows developers to define intuitive and custom behaviors for their user-defined types. This enhances code readability and enables seamless operations on complex objects. However, operator overloading should be used judiciously to ensure clarity and maintainability of the code.





Advertisement