Object EncapsulationTrail Index | Objects 2 | Object Encapsulation With the complete object, we can easily change things, taking advantage of the fact that its internal workings are now completely hidden from the outside world - a property know as encapsulation: /* * Ojects: hide internal change * * Assuming reasonably that the operator() is called more * often than getK and setK, * you may speed-up the calculation by * avoid calculating sqrt(k1) each time. * With the object, this change can be completely hidden from * the rest of of the code, which will not need any modification */ class Function { public: double operator()(double x) const { return sqK1*x+k2*x*x; } void setK1(double v) { if (v>0) sqK1=sqrt(v); } void setK2(double v) { k2=v; } private: double sqK1=0.; double k2=0.; } < Fully Grown Object | Trail Index | Inheritance > |