Files
ZUT-notatki/uczelnia/2_semestr/programowanie/laby/lab8/punkt.cpp
2026-04-27 12:00:49 +02:00

63 lines
1.3 KiB
C++

#include <iostream>
class Punkt2d {
public:
double x, y;
Punkt2d(double x, double y) : x(x), y(y) {}
Punkt2d() : x(0), y(0) {}
Punkt2d operator+=(const Punkt2d &inny_pkt) {
this->x += inny_pkt.x;
this->y += inny_pkt.y;
return *this;
}
Punkt2d operator-=(const Punkt2d &inny_pkt) {
this->x -= inny_pkt.x;
this->y -= inny_pkt.y;
return *this;
}
Punkt2d operator*=(const Punkt2d &inny_pkt) {
this->x *= inny_pkt.x;
this->y *= inny_pkt.y;
return *this;
}
Punkt2d operator+(const Punkt2d &inny_pkt) const {
Punkt2d p = *this;
p += inny_pkt;
return p;
}
Punkt2d operator-(const Punkt2d &inny_pkt) const {
Punkt2d pkt;
pkt.x = x - inny_pkt.x;
pkt.y = y - inny_pkt.y;
return pkt;
}
bool operator==(const Punkt2d &inny_pkt) const {
if (x != inny_pkt.x || y != inny_pkt.y) {
return false;
}
return true;
}
bool operator!=(const Punkt2d &inny_pkt) const {
return !(*this == inny_pkt);
}
friend std::ostream &operator<<(std::ostream &os, const Punkt2d &inny_pkt) {
os << '(' << inny_pkt.x << ',' << inny_pkt.y << ')';
return os;
}
};
int main() {
Punkt2d a(6, 7), b(3, 4);
Punkt2d c = a + b;
std::cout << c << std::endl; // (9, 11)
std::cout << (a == b) << std::endl; // false
std::cout << (a != a) << std::endl; // false
}