C++: Creating "abstract class" with implemented functions

I created a class to represent a vector (Vec3D), along with it’s basic canonical operations (sum, scalar product, cross product, etc.). I will use this class to derive classes of real physical units (position, velocity, acceleration, force, etc.) which will have their own "exclusive" characteristics and operations, *in addition to their vector operations.

I want Vec3D to be non-instanciable, since everything has to be a physical unit, but making it abstract would mean I have to re-define all the operators for each derived class again.

What would be the best practice here, that allows me to leverage the already defined operators? (maybe defining the constructors as private, and the operators as public?

and

In which case would I need to use virtual functions?

Below my base class:

class Vec3D
{
private:
  double _x;
  double _y;
  double _z;

protected:
  // Constructors / Destructor
  Vec3D(const double x, const double y, const double z);
  Vec3D();
  Vec3D(const Vec3D& source);
  Vec3D(Vec3D&& source);
  ~Vec3D(); // Declared just for completeness, although not necessary

public:
  // Operator Overloading
  Vec3D& operator=(const Vec3D& source);
  Vec3D& operator=(Vec3D&& source);
  Vec3D& operator+=(const Vec3D& source);
  Vec3D& operator+=(Vec3D&& source);
  Vec3D operator+(const Vec3D& source) const;
  Vec3D operator+(Vec3D&& source) const;
  Vec3D operator-() const;
  Vec3D& operator-=(const Vec3D& source);
  Vec3D& operator-=(Vec3D&& source);
  Vec3D operator-(const Vec3D& source)const;
  Vec3D operator-(Vec3D&& source) const;
  Vec3D operator*(const double source) const;
  double operator*(const Vec3D& source) const;
  double operator*(Vec3D&& source) const;
  Vec3D operator^(const Vec3D& source) const;
  Vec3D operator^(Vec3D&& source) const;
};

Thank you

Source: Windows Questions C++

LEAVE A COMMENT