In OOP, you typically have the validation occur in the function and have the function throw an exception if the value is not within your accepted range, in your case.
However, if you're doing this for a class and aren't expected to know about throwing/catching exceptions yet, you can do one of two things:
- Have your function return an int, bool, etc. If the argument is within range, then your function returns some value indicating thus. Else, the function returns some value indicating the argument was not within the range.
Something like this:
bool setRadius(double radius)
{
if(radius >= 0)
{
m_radius = radius;
return true;
}
else return false;
}
Notice that you don't initiate/change the value stored in m_radius if the argument of "radius" is not within your range. There would be no point in changing the value of m_radius to an unacceptable value -- it's a waste of time.
- Check it in main() ("in the dialog"?)
If you want to know more about exceptions, you can refer to this page for a greater understanding of what an exception is: http://www.cplusplus.com/doc/tutorial/exceptions/
So, i++ and to conclude, the convention is to check it in the method and have the method throw an exception if the value is out of the accepted range. You may not have to if you're in a class and haven't covered exceptions yet though. Best of luck to you! :)