169. Encapsulation

Question.1

A MotorDriver validates speed through a setter:

class MotorDriver {
   float duty_cycle;
public:
   void setSpeed(float speed) {
       if (speed > 90.0f) speed = 90.0f;
       if (speed < 0.0f)  speed = 0.0f;
       duty_cycle = speed;
   }
   float getSpeed() const { return duty_cycle; }
};

If duty_cycle were public, what could go wrong?

Need Help? Refer to the Quick Guide below

Select Answer

Restart quiz!