Answer to Question #286196 in C++ for hajar

Question #286196

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. 



1
Expert's answer
2022-01-10T00:24:50-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment