Ho migliorato ulteriormente la classe Point.
Così è una classe ben fatta? Ci sono errori grossolani? Manca qualche funzione importante?! Si può ancora migliorare?
Grazie per i preziosi consigli! =).
codice:
#ifndef POINT_H
#define POINT_H
#include <iostream>
using std::ostream;
using std::istream;
class Point
{
friend ostream &operator<<(ostream &, const Point &);
friend istream &operator>>(istream &, Point &);
public:
Point operator+=(const Point &);
Point operator-=(const Point &);
bool operator==(const Point &) const;
bool operator!=(const Point &) const;
Point();
Point(const double &, const double &);
~Point();
void setX(const double &);
void setY(const double &);
void setXY(const double &, const double &);
double getX() const;
double getY() const;
void move(const double &, const double &);
private:
double x;
double y;
};
double getDistance(const Point &);
double getDistance(const Point &, const Point &);
#endif
codice:
#include "point.h"
#include <iostream>
using std::endl;
using std::ostream;
using std::istream;
#include <cmath>
using std::pow;
using std::sqrt;
ostream &operator<<(ostream &output, const Point &point)
{
output << point.x << ' ' << point.y;
return output;
}
istream &operator>>(istream &input, Point &point)
{
input >> point.x;
input.ignore();
input >> point.y;
return input;
}
Point Point::operator+=(const Point &P2)
{
return Point(x + P2.x, y + P2.y);
}
Point Point::operator-=(const Point &P2)
{
return Point(x - P2.x, y - P2.y);
}
bool Point::operator==(const Point &P2) const
{
if (x == P2.x and y == P2.y)
{
return true;
}
return false;
}
bool Point::operator!=(const Point &P2) const
{
if (x != P2.x or y != P2.y)
{
return true;
}
return false;
}
Point::Point()
{
setX(0);
setY(0);
}
Point::Point(const double &X, const double &Y)
{
setX(X);
setY(Y);
}
Point::~Point()
{
}
void Point::setX(const double &X)
{
x = X;
}
void Point::setY(const double &Y)
{
y = Y;
}
void Point::setXY(const double &X, const double &Y)
{
x = X;
y = Y;
}
double Point::getX() const
{
return x;
}
double Point::getY() const
{
return y;
}
void Point::move(const double &X, const double &Y)
{
setX(x + X);
setY(y + Y);
}
double getDistance(const Point &P1)
{
return sqrt(pow(P1.getX(), 2) + pow(P1.getY(), 2));
}
double getDistance(const Point &P1, const Point &P2)
{
return sqrt(pow(P1.getX() - P2.getX(), 2) + pow(P1.getY() - P2.getY(), 2));
}