No, it's not necessary. It's perfectly OK to have "free functions" in C++: namely, ones that are declared outside of a class. Putting them in a class doesn't affect performance unless they are
virtual
functions. Instead, the justification is that the class would just be unnecessary boilerplate. And if such functions are added to a class that already has a legitimate purpose, they unneccesarily clutter its interface.
However, it's a good idea to define classes for things that can be measured differently. For example, a
Distance
class might have two members: an integral
distance_
, and the
units_
being used (
e.g., meters, kilometers, feet, miles). Here, the
units_
field itself should be represented by two fields: a
scale_
and the baseline
unit_
(
{1,m}
,
{1000,m}
,
{1,ft}
,
{5280,ft}
for the previous examples). This recent
article[
^] discusses this in some detail (it's C#, not C++, but it should still make sense).
It would definitely be advisable to define a
Date
class to hide the details of converting a date to particular calendar or display format (
e.g., should May 5th be shown as 5/6 or 6/5?). Similarly, there should be a class for
Coordinate
, which might even convert between Cartesian and polar coordinates.
Without these kinds of classes, the code becomes cluttered with copy-paste logic for converting between metric and Imperial, Gregorian and lunar, or Cartesian and polar.