Define a class Point to represent the x, y and z coordinates of a 3D Point. Overload the == and! = operators as friend functions to compare two point objects.
#include <iostream>
using namespace std;
class Point {
public:
float x;
float y;
float z;
Point(float _x, float _y, float _z) : x(_x), y(_y), z(_z) { }
friend bool operator== (const Point &p1, const Point &p2);
friend bool operator!= (const Point &p1, const Point &p2);
};
bool operator==(const Point &p1, const Point &p2) {
return p1.x == p2.x && p1.y == p2.y && p1.z == p2.z;
}
bool operator!=(const Point &p1, const Point &p2) {
return p1.x != p2.x || p1.y != p2.y || p1.z != p2.z;
}
int main() {
Point point1(0, 0, 0);
Point point2(1, 2, 3);
if (point1 == point2) {
cout << "point1 == point2" << endl;
} else if (point1 != point2) {
cout << "point1 != point2" << endl;
}
return 0;
}
Comments
Leave a comment