Page Content

Tutorials

What Are The Types Of Operator Overloading In C++?

Types of Operator Overloading in C++

Overloading Unary Operators

Unary operators have one operand. Examples include increment (++), decrement (–), unary minus (-), and logical NOT (!).

Implementation as a Member Function: Overloaded unary operators don’t need parameters. By referring to the object on which the operator is invoked, the this reference implicitly passes the single operand to the function.

Implementation as a Non-Member (Friend) Function: The implementation of a unary operator as a non-member (friend) function involves overloading it with a single explicit parameter, usually a reference to the operand. A reference (such as SomeType&) should be used as the parameter whenever the operator changes its operand (such as ++ or –).

Example: Overloading the Increment (++) Operator (Member Function)

The prefix (++obj) and postfix (obj++) are the two ways the ++ operator can appear. For both versions, you can create distinct implementations in C++. An extra, unused int parameter is required by the postfix operator function to differentiate the postfix version from the prefix (the compiler provides this parameter with a value of 0 when the postfix operator is used).

#include <iostream>
class SomeValue {
private:
    int data;
public:
    SomeValue(int val = 0) : data(val) {}
    void display() const {
        std::cout << "Data: " << data << std::endl;
    }
    // Prefix increment
    SomeValue& operator++() {
        ++data;
        return *this;
    }
    // Postfix increment
    SomeValue operator++(int) {
        SomeValue result = *this;
        ++(*this);
        return result;
    }
};
int main() {
    SomeValue sv1(10);
    std::cout << "Original sv1: "; sv1.display();
    ++sv1;
    std::cout << "After prefix ++sv1: "; sv1.display();
    SomeValue sv2(20);
    std::cout << "Original sv2: "; sv2.display();
    SomeValue sv3 = sv2++;
    std::cout << "After postfix sv2++:\n";
    std::cout << "sv2: "; sv2.display();
    std::cout << "sv3 (result of sv2++): "; sv3.display();
    return 0;
}

Output

Original sv1: Data: 10
After prefix ++sv1: Data: 11
Original sv2: Data: 20
After postfix sv2++:
sv2: Data: 21
sv3 (result of sv2++): Data: 20

Overloading Binary Operators

On two operands, binary operators operate. Common binary operators include assignment (=, +=), relational (==,!=, <, >, <=, >=), and arithmetic (+, -, *, /, %).

Implementation as a Member Function: A member function overloading a binary operator requires one parameter. This pointer implicitly passes the left operand but explicitly supplies the right. Left object calls operator function in operator expression.

Implementation as a Non-Member (Friend) Function: A friend function overloaded binary operator accepts two explicit parameters. First and second parameters denote left and right operands, respectively. Due to implicit type conversions on both the left and right operands, non-member functions are often used for symmetric operators like relational or arithmetic operators.

Example: Overloading the Addition (+) Operator (Member Function)

#include <iostream>
class Box {
public:
    double length;
    double breadth;
    double height;
    Box() : length(0.0), breadth(0.0), height(0.0) {}
    Box(double l, double b, double h) : length(l), breadth(b), height(h) {}
    double getVolume(void) {
        return length * breadth * height;
    }
    Box operator+(const Box& op2) {
        Box temp;
        temp.length = this->length + op2.length;
        temp.breadth = this->breadth + op2.breadth;
        temp.height = this->height + op2.height;
        return temp;
    }
};
int main() {
    Box Box1(6.0, 7.0, 5.0);        // Declare Box1
    Box Box2(12.0, 13.0, 10.0);  // Declare Box1
    Box Box3;                            // Declare Box3
    double volume;
    volume = Box1.getVolume();
    std::cout << "Volume of Box1 : " << volume << "\n";
    volume = Box2.getVolume();
    std::cout << "Volume of Box2 : " << volume << "\n";
    Box3 = Box1 + Box2;
    volume = Box3.getVolume();
    std::cout << "Volume of Box3 : " << volume << "\n";
    return 0;
}

Output

Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

You can also read Function Overloading In C++ Functions And Advantages

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index