我正在通过阅读Stroustrup的《C++程序设计原理与实践》来学习C++。
在关于前置条件和后置条件的部分,有一个函数的以下示例:
int area(int length, int width)
// calculate area of a rectangle;
// pre-conditions: length and width are positive
// post-condition: returns a positive value that is the area
{
if (length<=0 || width <=0)
error("area() pre-condition");
int a = length*width;
if (a<=0)
error("area() post-condition");
return a;
}
让我感到困惑的是这段代码的任务:
找到一对数值,使得此版本的area函数的前置条件成立,但后置条件不成立。
是否存在这样的整数数值,其前置条件可以成立但后置条件不成立?