Is it worth to write classes representing 1D, 2D, 3D points using templates
template <class T>
class Point2D
{
protected:
T X, Y;
public:
Point2D(const T x, const T y) : hot smileyx), Y(y)) {}
...
};
template <class T>
class Point3D : public Point2D<T>
{
protected:
T Z;
public:
Point3D(const T x, const T y, const T z) : Point2D<T>(x,y), Z(z) {}
...
};
...or using this approach:
class Point2D
{
protected:
double X, Y;
public:
Point2D(const double x, const double y) : X(x), Y(y)) {}
...
};
class Point3D : public Point2D
{
protected:
double Z;
public:
Point3D(const double x, const double y, const double z) : Point2D(x,y), Z(z) {}
...
};
We understand the coordinates as continuous variables, so it makes sense to express them using double values. A similar situation arises when working with matrices. However in this case templates are widely used...
This class is not only for single use, but it will be part of the library... And my second part of the question. How some "measure" functions should be implemented?
template <class T>
T getDist(const Point2D <T> * p1, const Point2D<T> *p2)
{
....
}
or
double getDist(const Point2D <T> * p1, const Point2D<T> *p2)
{
....
}
Is it reasonable to write such function in general or for some specific type?