Things are even simpler if we use C++ modules. In that case, we donโt need to make separate .h and .cpp files. And the only thing we generally need to export other than the module itself is the class we are defining.
We may still wish to place the function definitions outside of the class. That way, the class definition has the minimal amount of code necessary and sticks to just what an โoutside userโ of the code should care about. The implementation details are there, but out of the way. But all of this code can go into a single file like Point.cxx:
// Start global module fragment
module;
// All includes go here
#include <cmath>
// Start module, declare its name and make available outside this module
export module Point;
/**
* @brief Represents a point in 2D space.
*
*/
export class Point {
public:
/**
* @brief Construct a new Point object
*
* @param x starting x coordinate
* @param y starting y coordinate
*/
Point(double x, double y);
/**
* @brief Get the x coordinate of the point
*
* @return double x coordinate
*/
double getX();
private:
double m_x;
double m_y;
};
// Implementations
Point::Point(double x, double y)
{
m_x = x;
m_y = y;
}
double Point::getX()
{
return m_x;
}
Note16.13.2.
It is still possible to separate the module into an interface file and an implementation file if we want to do so. However, with modules, there is not the same technical need to do so.