The convention for expressing OO in C++ is to bundle functions with the data. The functions are called class member functions and have an implicit 'this' pointer, in contrast to free functions.
Nevertheless in CEDA member functions on models and variants are not supported. For example the following will generate a compiler error:
$model Rectangle
{
Point p1;
Point p2;
// Error : member functions in models are not supported
float32 Area() const { return (p2.y - p1.y) * (p2.x - p1.x); }
};
It is noteworthy that for ceda data model types, the data members are already public, so encapsulation doesn't provide the excuse to bundle methods with the data.
Instead free functions are used such as
float32 Area(const Rectangle& r)
{
return (r.p2.y - r.p1.y) * (r.p2.x - r.p1.x);
}
Free functions can be defined by other parties independently of whoever wrote the model or variant data types (perhaps in a different software library). Free functions are the better choice for defining operations on value types because all arguments are treated equally for the purposes of implicit conversions. E.g. If Rectangle has an implicit conversion from Square then a free function with signature Area(Rectangle) can be passed a Square value. This doesn't work when Area() is a class member function.
Overloading of free functions (i.e. adhoc polymorphism) is encouraged. For example, there may be many functions named 'Area' that give the area of a 2D shape:
float32 Area(const Point& r);
float32 Area(const Line& r);
float32 Area(const Square& r);
float32 Area(const Rectangle& r);
float32 Area(const Circle& r);
float32 Area(const Polygon& r);
float32 Area(const ConvexPolygon& r);