Unary Operator Overloading in C++

 


1️⃣ What is Unary Operator Overloading?

Unary operators operate on a single operand (e.g., ++, --, -, !).
Operator overloading allows custom behavior for these operators when used with user-defined types (like classes).

Key Features:

✅ Overloads operators like ++, --, -, !, ~
✅ Uses the operator keyword
✅ Can be implemented as a member function or a friend function


2️⃣ Syntax of Unary Operator Overloading

ReturnType operator Symbol() {
    // Overloaded operator logic
}

🔹 Symbol is the operator to overload (e.g., ++, -).
🔹 ReturnType is usually the class type.


3️⃣ Example: Overloading - Operator

Negating a Number

#include <iostream>
using namespace std;

class Number {
private:
    int value;

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

    // Overload unary - operator
    Number operator-() {
        return Number(-value);
    }

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

int main() {
    Number n1(10);
    Number n2 = -n1; // Calls overloaded - operator

    n1.display();
    n2.display();
    return 0;
}

🔹 Output

Value: 10
Value: -10

4️⃣ Example: Overloading ++ (Prefix and Postfix)

Incrementing a Counter

#include <iostream>
using namespace std;

class Counter {
private:
    int count;

public:
    Counter(int c = 0) : count(c) {}

    // Overload Prefix ++
    void operator++() {
        ++count;
    }

    // Overload Postfix ++
    void operator++(int) {
        count++;
    }

    void display() {
        cout << "Count: " << count << endl;
    }
};

int main() {
    Counter c1(5);

    ++c1;  // Prefix increment
    c1.display();

    c1++;  // Postfix increment
    c1.display();

    return 0;
}

🔹 Output

Count: 6
Count: 7

5️⃣ Overloading -- (Decrement) Operator

#include <iostream>
using namespace std;

class Counter {
private:
    int count;

public:
    Counter(int c = 0) : count(c) {}

    // Overload Prefix --
    void operator--() {
        --count;
    }

    // Overload Postfix --
    void operator--(int) {
        count--;
    }

    void display() {
        cout << "Count: " << count << endl;
    }
};

int main() {
    Counter c(5);

    --c; // Prefix decrement
    c.display();

    c--; // Postfix decrement
    c.display();

    return 0;
}

🔹 Output

Count: 4
Count: 3

6️⃣ Overloading Logical NOT (!) Operator

#include <iostream>
using namespace std;

class Check {
private:
    bool status;

public:
    Check(bool s) : status(s) {}

    // Overload ! operator
    bool operator!() {
        return !status;
    }

    void display() {
        cout << "Status: " << status << endl;
    }
};

int main() {
    Check obj(true);

    obj.display();
    cout << "Negated Status: " << !obj << endl;

    return 0;
}

🔹 Output

Status: 1
Negated Status: 0

7️⃣ Summary

Unary Operator Overloading allows customization of ++, --, -, !, etc.
✅ Can be implemented as a member function or friend function.
✅ Supports prefix (++x) and postfix (x++) versions separately.
✅ Useful for counter classes, Boolean logic, and mathematical operations.

Would you like a real-world example like Matrix Negation? 🚀

Post a Comment

0 Comments