example3_not=_and_==.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

class Vector {
private:
    int x, y;
public:
    Vector(int x = 0, int y = 0) : x(x), y(y) {}

    // Overload the == operator
    bool operator==(const Vector& v) const {
        return (x == v.x && y == v.y);
    }

    // Overload the != operator
    bool operator!=(const Vector& v) const {
        return !(*this == v);
    }
};

int main() {
    Vector v1(3, 4);
    Vector v2(3, 4);
    Vector v3(5, 6);

    if (v1 == v2) {
        std::cout << "v1 is equal to v2" << std::endl;
    }

    if (v1 != v3) {
        std::cout << "v1 is not equal to v3" << std::endl;
    }

    return 0;
}
Back to operator_overloading