Operator Overloading in C++ as int + obj

Implement the operator overloading outside of the class:

class Num
{
public:
    Num(int i)
    {
        this->i = i;
    }

    int i;
};

int operator+(int i, const Num& n)
{
    return i + n.i;
}

Leave a Comment