Say I create a class Complex
with members r
and phi
that writes complex numbers in polar form through the constructor
Complex (double Re=0, double Im=0) : r(sqrt(Re*Re + Im*Im)), phi(atan2(Im,Re)) {}
In the class I find component functions like
double real() const
{return r*cos(phi);}
which seem logical, but I do not understand the neccessity of const
(is there a difference between const double real()
and double real() const
?)
I have been told that it is important such that the component function does not modify the class.
Modify in what sense? Surely no members of the class will be added or removed because of a component function, and how would const
actually avert this? Any help is greatly appreciated.