Overloading unary and binary operators

Operator overloading is a powerful feature in C++ that allows you to redefine the behavior of operators for user-defined types. By overloading operators, you can make your custom classes behave like built-in types, providing intuitive and natural syntax for operations.

Overloading Unary Operators

Unary Operators Overview

Unary operators operate on a single operand. Examples include increment (++), decrement (–), logical NOT (!), and unary minus (-).

Overloading Unary Operators in C++

Unary operators can be overloaded as member functions or as standalone functions. Let’s explore an example of overloading the unary minus (-) operator for a custom class Vector.

				
					#include <iostream>

class Vector {
private:
    int x, y;

public:
    // Constructor to initialize the object's value
    Vector(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }

    // Overloading the unary minus operator
    Vector operator-() const {
        return Vector(-x, -y);
    }

    // Display function to print the vector
    void display() const {
        std::cout << "(" << x << ", " << y << ")" << std::endl;
    }
};

int main() {
    Vector v1(3, 4);

    // Using the overloaded unary minus operator
    Vector v2 = -v1;

    // Displaying the result
    std::cout << "Original Vector: ";
    v1.display(); // Output: (3, 4)
    std::cout << "Negated Vector: ";
    v2.display(); // Output: (-3, -4)

    return 0;
}

				
			

Explanation:

  • In this example, we define a class Vector representing a 2D vector with x and y components.
  • We overload the unary minus operator (-) as a member function of the Vector class.
  • Inside the overloaded operator function, we negate both the x and y components of the vector.
  • In the main() function, we create a Vector object v1 with components (3, 4).
  • We then use the overloaded unary minus operator to negate v1, resulting in a new Vector object v2.
  • Finally, we display the original and negated vectors to demonstrate the effect of the unary minus operator overloading.

Overloading Binary Operators

Binary Operators Overview

Binary operators operate on two operands. Examples include addition (+), subtraction (), multiplication (*), and division (/).

Overloading Binary Operators in C++

Binary operators can also be overloaded as member functions or as standalone functions. Let’s explore an example of overloading the addition (+) operator for the Vector class.

				
					#include <iostream>

class Vector {
private:
    int x, y;

public:
    // Default constructor
    Vector() {
        x = 0;
        y = 0;
    }

    // Parameterized constructor
    Vector(int x, int y) {
        this->x = x;
        this->y = y;
    }

    // Overloading the addition operator
    Vector operator+(const Vector& other) const {
        return Vector(x + other.x, y + other.y);
    }

    // Display function to print the vector
    void display() const {
        std::cout << "(" << x << ", " << y << ")" << std::endl;
    }
};

int main() {
    Vector v1(3, 4);
    Vector v2(2, 5);

    // Using the overloaded addition operator
    Vector v3 = v1 + v2;

    // Displaying the result
    std::cout << "Vector 1: ";
    v1.display(); // Output: (3, 4)
    std::cout << "Vector 2: ";
    v2.display(); // Output: (2, 5)
    std::cout << "Sum: ";
    v3.display(); // Output: (5, 9)

    return 0;
}

				
			

Explanation:

  • In this example, we continue with the Vector class representing a 2D vector.
  • We overload the addition operator (+) as a member function of the Vector class.
  • Inside the overloaded operator function, we perform component-wise addition of two vectors.
  • In the main() function, we create two Vector objects v1 and v2 with components (3, 4) and (2, 5) respectively.
  • We then use the overloaded addition operator to add v1 and v2, resulting in a new Vector object v3.
  • Finally, we display the original vectors and their sum to demonstrate the effect of the addition operator overloading.

In this chapter, we explored the concept of overloading unary and binary operators in C++. We learned how to redefine the behavior of operators for user-defined types, enhancing the expressiveness and readability of our code. By overloading operators, we can create more intuitive and natural syntax for operations on custom classes, making our code more powerful and flexible.Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India