*
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point() : x(0), y(0) {} // domyślny konstruktor ustawia zmienne x i y na 0;
Point(const double x, const double y) : x(x), y(y) {} // konstruktor z parametrami,
Point(const Point & rhs) {x = rhs.x; y = rhs.y;} // jawny konstruktor kopiujący
virtual ~Point() {} // wirtualny destruktor - wrazie gdyby ktoś chciał dziedziczyć po naszej klasie
bool operator== (const Point & rhs) { return (x == rhs.x && y == rhs.y);} //operator porównania, zwraca true jeśli porownywane pkt sa identyczne
Point operator+ (const Point & rhs) { return Point(x + rhs.x, y + rhs.y);} //operator dodawania +
Point operator- (const Point & rhs) { return Point(x - rhs.x, y - rhs.y);} //operator odejmowania -
Point operator* (const Point & rhs) { return Point(x * rhs.x, y * rhs.y);} //operator mnożenia *
Point& operator+= (const Point & rhs) {this->x += rhs.x; this->y += rhs.y; return *this;} // operator dodawania do biezacego elementu +=;
Point& operator-= (const Point & rhs) {this->x -= rhs.x; this->y -= rhs.y; return *this;} // operator odejmowania od biezacego elementu -=;
Point& operator*= (const Point & rhs) {this->x *= rhs.x; this->y *= rhs.y; return *this;} // operator mnozenia biezacego elementu *=;
void setX(const double x) {this->x = x;} //ustaw x;
void setY(const double y) {this->y = y;} //ustaw y;
double getX() const {return x;} // pokaz x;
double getY() const {return y;} // pokaz y;
protected:
double x;
double y;
};
class Segment
{
public:
Segment() : a(Point()), b(Point()) {} // domyślny konstruktor
Segment(const Point a, const Point b) : a(a), b(b) {} // konstruktor z parametrami
Segment(const Segment & rhs) {a = rhs.a; b = rhs.b;} // jawny konstruktor kopiujacy
virtual ~Segment() {} // wirtualny destruktor;
void setA(const Point & a) {this->a = a;} // ustaw koniec A
void setB(const Point & b) {this->b = b;} // ustaw koniec B
Point getA() const {return a;} // pokaz koniec A
Point getB() const {return b;} // pokaz koniec B
double Length() const {return sqrt(pow(b.getX() - a.getX(), 2) + pow(b.getY() - a.getY(), 2));} //oblicz dlugosc
protected:
Point a; //koniec a
Point b; //koniec b
};
int main()
{
// Przyklad uzycia
// Tworzymy 2 punkty i obliczamy dlugosc danego odcinka
double x, y; // zmienne pomocnicze
cout << "Podaj x i y pierwszego punktu: ";
cin >> x >> y;
Point p1(x, y); // tworzenie pierwszego punktu z znanych wartosci
cout << "Podaj x i y drugiego punktu: ";
cin >> x >> y;
Point p2(x, y); // tworzenie drugiego punktu;
if(p1 == p2) // error ! odcinek ma dwa rozne konce
{
cout << "Z podanych punktow nie mozna zbudowac odcinka";
return -1;
}
Segment s(p1, p2); // tworzymy odcinek
cout << "Dlugosc odcinka to: " << s.Length() << endl;
// zmienimi warotsc jednego konca;
p1 += Point(5, 0);
s.setA(p1);
cout << "Dlugosc odcinka teraz to: " << s.Length() << endl;
cout << "by meViu" << endl;
return 0;
}