If I have a simple class, for example ...
class Rectangle { private: double length; double width; public: void setLength(double l) { length = l; } void setWidth(double w) { width = l; } void getLength() { return length; } void getWidth() { return width; } void getArea() { return length * width; } };
... and the person using it calls the setWidth () mutator with an invalid argument, say -1.0, what is the correct way to handle this? When I say correctly, for example, should I change the type of the return value from void to bool or, possibly, to int, and return a value to indicate whether the operation was successful or should I allow the value to be set, since in theory this will not break nothing, but any values returned as a result of, say, from getArea () will be garbage, and the user will have to debug what he / she did wrong?
Sorry if this is a stupid question, but two text books are later, and I'm still not quite sure how the classes that I write should be implemented for other users.
user898058
source share