If you can make a function const, you should do it. You should also pass parameters that are not basic types by const reference if they are not modified instead of passing them by value.
But there is a problem with your example:
The function
avgSquare
is defined as returning a
square
but your implementation returns a
double
or an
int
.
A possible solution might be:
int area() const;
int avgSquare(const square& s1, const square& s2) const
{
return (s1.area() + s2.area()) / 2;
}
int avgSquare(const square& other) const
{
return avgSquare(*this, other);
}
If you need to return a
double
just divide by 2.0:
double avgSquare(const square& s1, const square& s2) const
{
return (s1.area() + s2.area()) / 2.0;
}
There are only a few simple operations so that there is no need to use other member functions like
plus()
.