Jaewoo Song
Jaewoo Song

Categories

  • Tech

Simply saying, this is about re-defining basic operators for primitive types and it can be used for operating between objects from classes or structs.


Let’s see this example.

#include <iostream>

using namespace std;

class Point {
    public:
        int x;
        int y;

        Point(int _x, int _y) {
            x = _x;
            y = _y;
        }

        void print() {
            cout<<"(x, y): ("<<x<<", "<<y<<")"<<"\n";
        }

        Point operator + (Point& other) {
            int new_x = x + other.x;
            int nex_y = y + other.y;
            return Point(new_x, nex_y);
        }

        Point operator - (Point& other) {
            int new_x = x - other.x;
            int nex_y = y - other.y;
            return Point(new_x, nex_y);
        }

        bool operator > (Point& other) {
            return x > other.x;
        }

        bool operator < (Point& other) {
            return x < other.x;
        }

        bool operator == (Point& other) {
            return x == other.x;
        }
};

int main() {

    Point p1 (1, 2);
    Point p2 (4, 5);

    cout<<"p1 ";
    p1.print();

    cout<<"p2 ";
    p2.print();

    Point p3 = p1 + p2;
    Point p4 = p1 - p2;

    cout<<"p3 ";
    p3.print();

    cout<<"p4 ";
    p4.print();

    if (p1 > p2) {
        cout<<"p1 is bigger"<<"\n";
    } else if (p1 < p2) {
        cout<<"p2 is bigger"<<"\n";
    } else {
        cout<<"p1 and p2 is equal"<<"\n";
    }

    return 0;
}


I defined operators for adding, subtracting and multiplying between Point objects.

Of course, it can be different for a specific use but in conclusion, we just return what should be returned after each calculation.

The point is that the left operand is the basis and the right one is put as a parameter.


This is the result of the above example.

The result of operation overloading above.