Operator Overloading/Unary operator

Estimated reading: 7 minutes 37 Views

In C++, operator overloading refers to the ability to redefine the behavior of operators such as arithmetic, logical, and relational operators for user-defined types. Unary operators are those operators that operate on a single operand. Overloading unary operators allows you to customize their behavior when applied to objects of your own classes.

Purpose of Operator Overloading:

  1. Provide Natural Syntax: Operator overloading allows you to use operators with user-defined types in a way that mimics their usage with built-in types. For example, you can add two objects of a class using the + operator, concatenate strings using the + operator, or increment an object using the ++ operator.
  2. Enhance Readability: By overloading operators, you can make your code more intuitive and readable, as it allows you to express operations in a natural and concise way.

Example 1:

This C++ code demonstrates the overloading of the unary ++ operator for a class called Unary. Let’s break down the code:

#include <iostream>
using namespace std;

class Unary {
    int x, y;
public:
    Unary(int i = 0, int j = 0) {
        x = i;  
        y = j;
    }
    
    void show() {
        cout << x << " " << y << endl;
    }

    // Overloading the unary ++ operator as a member function
    void operator++() {
        x++;
        y++;
    }
};

int main() {
    Unary v(10, 20);
    v++; // Incrementing the object 'v' using overloaded ++ operator
    v.show(); // Displaying the updated values of 'x' and 'y'

    return 0;
}

Explanation:

  • The Unary class has two private member variables x and y, and a constructor to initialize them. Additionally, it has a member function show() to display the values of x and y.
  • Inside the class definition, the unary ++ operator is overloaded using the operator++() member function. This function increments the values of x and y.
  • In the main() function, an object v of the Unary class is created with initial values 10 for x and 20 for y.
  • The ++ operator is applied to the object v using v++. Since it’s a unary operator, it doesn’t require any operand.
  • The overloaded operator++() function increments the values of x and y by one.
  • After the increment operation, the show() function is called to display the updated values of x and y.

The output of the code will be:

11 21

This is because the values of x and y are incremented by 1 after the ++ operator is applied to the object v. Thus, the updated values are displayed as 11 for x and 21 for y.

Example 2:

#include <iostream>
using namespace std;
class Unary {
        int x, y;
    public:
        Unary(int i = 0, int j = 0) {
            x = i;  y = j;
        }
    void show() { cout << x << " " << y << endl; }
    Unary operator++()
    {
        x++;
        y++;
        return *this;
    }
    Unary operator++(int)
    {
        Unary t;
        t = *this;
        x++;
        y++;
        return t;
    }
};
int main()
{
    Unary v(10, 20), k;
    v++;
    k = v++;
    k.show();
    v.show();

    return 0;
}

This C++ code demonstrates the overloading of the unary ++ operator for a class called Unary, along with the postfix increment operator ++ that takes an additional dummy integer parameter. Let’s break down the code:

  • The Unary class has two private member variables x and y, and a constructor to initialize them. Additionally, it has a member function show() to display the values of x and y.
  • Inside the class definition, the unary ++ operator is overloaded twice:
    1. The operator++() function overloads the prefix ++ operator. It increments the values of x and y and returns the updated object.
    2. The operator++(int) function overloads the postfix ++ operator with an additional dummy integer parameter. It creates a temporary object t and assigns the current object’s values to it. Then, it increments the values of x and y of the current object and returns the temporary object t.
  • In the main() function, two objects v and k of the Unary class are created, with v initialized with values 10 for x and 20 for y.
  • v++ is applied to increment v using the postfix increment operator. This will increment the values of x and y of v and return a temporary object with the original values.
  • k = v++ will first assign the temporary object (with original values) to k and then increment v using the postfix increment operator.
  • The show() function is called to display the values of k and v after the increment operations.

The output of the code will be:

11 21
12 22

Example 3:

#include <iostream>
using namespace std;

class Unary {
    int x, y;
public:
    Unary(int i = 0, int j = 0) {
        x = i;  
        y = j;
    }
    
    void show() {
        cout << x << " " << y << endl;
    }

    // Overloading the prefix ++ operator
    Unary operator++() {
        x++;
        y++;
        return *this;
    }

    // Overloading the postfix ++ operator with an additional dummy integer parameter
    Unary operator++(int) {
        Unary t;
        t = *this;
        x++;
        y++;
        return t;
    }

    // Overloading the unary - operator
    Unary operator-() {
        x = -x;
        y = -y;
        return *this;
    }
};

int main() {
    Unary k(1, 2); // Create an object of the class Unary with initial values 1 and 2
    -k; // Applying unary - operator to object k
    k.show(); // Display the updated values of k

    return 0;
}
  • The operator-() function overloads the unary – operator. It negates the values of x and y by multiplying them by -1 and returns the updated object.
  • In the main() function, an object k of the Unary class is created with initial values 1 for x and 2 for y.
  • The unary – operator is applied to the object k using -k. This will negate the values of x and y of k.
  • Finally, the show() function is called to display the updated values of x and y.

This indicates that the values of x and y of object k have been negated by applying the unary – operator. Therefore, x becomes -1 and y becomes -2.

Example 4:

#include <iostream>
using namespace std;
class Unary {
        int x, y;
    public:
        Unary(int i = 0, int j = 0) {
            x = i;  y = j;
        }
    void show() { cout << x << " " << y << endl; }
    Unary operator++()
    {
        x++;
        y++;
        return *this;
    }
    Unary operator++(int)
    {
        Unary t;
        t = *this;
        x++;
        y++;
        return t;
    }
    Unary operator-()
    {
        x = -x;
        y = -y;
        return *this;
    }
    bool operator!()
    {
        return (x == 0 && y == 0);
    }
};
int main()
{
    Unary x(1, 1);
    if (!x)
        cout << "true";
    else
        cout << "false";

    return 0;
}
  • The operator!() function overloads the logical negation operator !. It returns true if both x and y are equal to 0, indicating that the object is logically false. Otherwise, it returns false.
  • In the main() function, an object x of the Unary class is created with initial values 1 for x and 1 for y.
  • The logical negation operator ! is applied to the object x using !x. If x is logically false (i.e., both x and y are 0), it prints “true”. Otherwise, it prints “false”.

The output of the code will be:

false

Example 5:

#include <iostream>
using namespace std;
class Unary {
        int x, y;
    public:
        Unary(int i = 0, int j = 0) {
            x = i;  y = j;
        }
    void show() { cout << x << " " << y << endl; }
    Unary operator++()
    {
        x++;
        y++;
        return *this;
    }
    Unary operator++(int)
    {
        Unary t;
        t = *this;
        x++;
        y++;
        return t;
    }
    Unary operator-()
    {
        x = -x;
        y = -y;
        return *this;
    }
    bool operator!()
    {
        return (x == 0 && y == 0);
    }
    Unary operator+=(Unary b2)
    {
        x += b2.x;
        y += b2.y;
        return *this;
    }
};
int main()
{
    Unary b(1, 2), b2(3, 3);
    b += b2;
    b.show();
    
    return 0;
}

the operator+=() function overloads the compound assignment operator +=. It adds the corresponding member variables of the current object (this) with the member variables of the passed object (b2) and returns the updated object.

The output of the code will be:

4 5

 

Share this

Operator Overloading/Unary operator

Or copy link

CONTENTS
English