Unary Operator Overloading in CPP using both member & Friend function

Understanding working of unary Operator Overloading  using both member & Friend function in C++

As like Binary operators who operates on two operates on two operands simi8larly unary operators are the operators which operate on a single operand.

Ex:

  • ++(Increment operator),
  • –(decrement operator),
  • !(not operator)
  • etc.

We will see how to overload unary operators in C++ for an user-defined class.

Overloading Unary Operators for User Defined classes :

We will be using the following class as example

class

ComplexNumber

{
    int realNumber;
    int imaginaryNumber;

public:
    ComplexNumber() : realNumber(

0

), imaginaryNumber(

0

)
    {
    }
    ComplexNumber(int r, int i) : realNumber(r), imaginaryNumber(i)
    {
    }
};

We will load the – (minus operator) in two ways.

Method-1 : Overload Unary Minus (-) Operator using class Member function :

To overload the unary operator we can use the member function, and use it as an operand by objects.

#include <iostream>
#include <string>
#include <assert.h>
//Class ComplexNumber containing our overloaded minus operator
class ComplexNumber
{
    int realNumber;
    int imaginaryNumber;

public:
    ComplexNumber() : realNumber(0), imaginaryNumber(0)
    {
    }
    ComplexNumber(int r, int i) : realNumber(r), imaginaryNumber(i)
    {
    }
    void print()
    {
        int img = imaginaryNumber < 0 ? -imaginaryNumber : imaginaryNumber;
        std::cout << realNumber << (imaginaryNumber < 0 ? " - " : " + ") << "i" << img << std::endl;
    }
    // Overloaded unary minus operator prototype
    ComplexNumber operator-() const;
};

/*The minus operator was overloaded
It retuens a new object
*/
ComplexNumber ComplexNumber::operator-() const
{
    return ComplexNumber(-(this->realNumber), -(this->imaginaryNumber));
}

int main()
{
    // Creating an object of the complexNumber class
    ComplexNumber complex1(2, -3);
    std::cout << "complex number 1 = ";
    complex1.print();
    // Call the unary operator minus on complex1 and
    // store the returned in a new object
    ComplexNumber complex2 = -complex1;
    std::cout << "complex number 2 = ";
    complex2.print();
    return 0;
}
Output :
complex number 1 = 2 - i3
complex number 2 = -2 + i3

Method-2 Overload Unary Minus (-) Operator using Global Friend Function :

In the previous method we didn’t have to pass arguments into the function but in this method we will be using the global friend function to overload the unary operator so we have to pass as arguments.

#include <iostream>
#include <string>
#include <assert.h>
//Class ComplexNumber containing our overloaded minus operator
class ComplexNumber
{
    int realNumber;
    int imaginaryNumber;

public:
    ComplexNumber() : realNumber(0), imaginaryNumber(0)
    {
    }
    ComplexNumber(int r, int i) : realNumber(r), imaginaryNumber(i)
    {
    }
    void print()
    {
        int img = imaginaryNumber < 0 ? -imaginaryNumber : imaginaryNumber;
        std::cout << realNumber << (imaginaryNumber < 0 ? " - " : " + ") << "i" << img << std::endl;
    }
    //Making the global operator a friend
    friend ComplexNumber operator-(const ComplexNumber &obj);
};

/*The minus operator was overloaded as a global function
It retuens a new object
*/
ComplexNumber operator-(const ComplexNumber &obj)
{
    return ComplexNumber(-(obj.realNumber), -(obj.imaginaryNumber));
}

int main()
{
    // Creating an object of the complexNumber class
    ComplexNumber complex1(2, -3);
    std::cout << "complex number 1 = ";
    complex1.print();
    // Call the unary operator minus on complex1 and
    // store the returned in a new object
    ComplexNumber complex2 = -complex1;
    std::cout << "complex number 2 = ";
    complex2.print();
    return 0;
}

Output :
complex number 1 = 2 - i3
complex number 2 = -2 + i3