Two predicated methods.
Supporting Design by Contract in C++
by David Maley and Ivor Spence
Listing 1. Two predicated methods.
Complex Complex::operator/(const double& d)
{
if (!Invariant())
throw BadComplex(*this);
if (d==0)
throw DivideByZero();
Complex old(*this);
Complex result = // code;
if (result*d!=old)
throw BadComplexDivide(*this);
if (!Invariant())
throw BadComplex(*this);
return result;
}
T Stack<T>::pop()
{
if (!Invariant())
throw BadStack<T>(*this);
if (count()<=0)
throw EmptyStack();
Stack old(*this);
T result = // code;
if (result!=old.top() || count()!=old.count()-1)
throw BadPop(*this);
if (!Invariant())
throw BadStack<T>(*this);
return result;
}
About the Authors
David Maley is with St. Mary's University College, Belfast. He can be contacted at [email protected].
Ivor Spence is with Queen's University of Belfast. He can be contacted at [email protected].