Operator Overloading: Overloading Unary Minus Operator as Member Function

What is it?

Overloading Unary Minus Operator in C++ is the process of providing a new definition for the unary minus (-) operator when it is used with objects of a user-defined class. The unary minus operator is a mathematical operator that negates the value of its operand. When overloaded, it can be used to change the behavior of the operator based on your class requirements. This is done using a member function.


Syntax

The syntax of overloading the unary minus operator using a member function is:

ClassName operator-();

Where,

  • ClassName: It is the name of the class where the unary minus operator is overloaded.
  • -: It is the unary minus operator that is being overloaded.



How to use it

To overload the unary minus operator, follow the below steps:

  1. Define a class and its properties.
  2. Implement a member function with the keyword operator- to overload the unary minus operator.
  3. Inside the function, define the operation that the unary minus operator should perform when used with objects of your class.



Program code snippet example

#include <iostream>

using namespace std;

class Integer {
private:
    int value;

public:
    Integer(int v): value(v) {}

    // Overloading the unary minus operator.
    Integer operator-() {
        return Integer(-value);
    }

    void display() {
        cout << "Value: " << value << endl;
    }
};

int main() {

    // Creating an object of the class.
    Integer myInt(10);

    // Using the unary minus operator with the object.
    Integer negInt = -myInt;

    cout << "Original Integer: ";
    myInt.display();

    cout << "Negated Integer: ";
    negInt.display();

    return 0;
}

Output:

Original Integer: Value: 10
Negated Integer: Value: -10



Example:

#include <iostream>

class Cents {
private:
    int m_cents;
public:
    Cents(int cents) { m_cents = cents; }

    Cents operator-() const {
        return Cents(-m_cents);
    }

    int get_cents() const { return m_cents; }
};

int main() {
    const Cents nickle(5);
    std::cout << (-nickle).get_cents() << " cents\n";

    return 0;
}

Output:

-5 cents



Example:

#include <iostream>

class Cents {
private:
    int m_cents;
public:
    Cents(int cents = 0);

    Cents operator-() const;

    int get_cents() const;
};

Cents::Cents(int cents) : m_cents(cents) {
    m_cents = cents;
}

Cents Cents::operator-() const {
    return Cents(-m_cents);
}

int Cents::get_cents() const {
    return m_cents;
}

int main() {
    const Cents nickle(5);
    std::cout << (-nickle).get_cents() << " cents\n";

    return 0;
}

Output:

-5 cents



Important to know

  • Operator overloading allows programmers to use traditional operators (like +, -, *, etc.) in a way that is intuitive and easy to understand.
  • Overloading the unary minus operator allows you to define its behavior when used with objects of your class.
  • The unary minus operator returns a new object that is the negation of the original object.
  • Always return a new object from the unary minus operator overload function instead of modifying the original object. This aligns with the conventional use of the unary minus operator.



Best practices

When overloading the unary minus operator, it is important to keep the following best practices in mind:

  1. Keep it Intuitive: Operators should act in a way that is intuitive and aligns with their conventional use. This helps to maintain the readability of the code.

  2. Avoid Side Effects: Operators should avoid causing side effects. For the unary minus operator, this means it should return a new object instead of modifying the existing one.

  3. Consistency: If you're overloading one operator, you should consider overloading the related operators as well (like unary plus, if it makes sense to your class).

  4. Clear and Concise: The code inside the operator overload function should be as clear and concise as possible.




Overload the unary minus operator for the Integer class.