Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
2.7 kB
2
Indexable
Never
#include <iostream>
#include "point.h"

int main() {
    // Create Point objects using the parameterized constructor
    Point start(5, 10);
    Point end(50, 100);
    Point newPoint(10, 10);

    newPoint = start - end;
    std::cout << newPoint.getX() << "," << newPoint.getY() << std::endl;

    newPoint = start - 10;
    std::cout << newPoint.getX() << "," << newPoint.getY() << std::endl;

    newPoint = 40 - start;
    std::cout << newPoint.getX() << "," << newPoint.getY() << std::endl;

    // Test relational operators
    std::cout << std::boolalpha; // Print bools as true/false

    std::cout << "start < end: " << (start < end) << std::endl;
    std::cout << "start > end: " << (start > end) << std::endl;
    std::cout << "start <= end: " << (start <= end) << std::endl;
    std::cout << "start >= end: " << (start >= end) << std::endl;
    std::cout << "start == end: " << (start == end) << std::endl;
    std::cout << "start != end: " << (start != end) << std::endl;

    return 0;
}





#include "point.h"

// Constructor
Point::Point(int x, int y) : x(x), y(y) {}

// Getter methods
int Point::getX() const {
    return x;
}

int Point::getY() const {
    return y;
}

// Operator overloads
Point Point::operator-(const Point& rhs) const {
    return Point(this->x - rhs.x, this->y - rhs.y);
}

Point Point::operator-(int rhs) const {
    return Point(this->x - rhs, this->y - rhs);
}

Point operator-(int lhs, const Point& rhs) {
    return Point(lhs - rhs.x, lhs - rhs.y);
}

// Relational operators
bool Point::operator<(const Point& rhs) const {
    return (this->x < rhs.x) || (this->x == rhs.x && this->y < rhs.y);
}

bool Point::operator>(const Point& rhs) const {
    return (this->x > rhs.x) || (this->x == rhs.x && this->y > rhs.y);
}

bool Point::operator<=(const Point& rhs) const {
    return !(*this > rhs);
}

bool Point::operator>=(const Point& rhs) const {
    return !(*this < rhs);
}

bool Point::operator==(const Point& rhs) const {
    return (this->x == rhs.x) && (this->y == rhs.y);
}

bool Point::operator!=(const Point& rhs) const {
    return !(*this == rhs);
}



#ifndef POINT_H
#define POINT_H

class Point {
private:
    int x, y;

public:
    Point(int x, int y);
    int getX() const;
    int getY() const;

    Point operator-(const Point& rhs) const;
    Point operator-(int rhs) const;

    friend Point operator-(int lhs, const Point& rhs);
    bool operator<(const Point& rhs) const;
    bool operator>(const Point& rhs) const;
    bool operator<=(const Point& rhs) const;
    bool operator>=(const Point& rhs) const;
    bool operator==(const Point& rhs) const;
    bool operator!=(const Point& rhs) const;
};

#endif // POINT_H
Leave a Comment