|
|
|
|
|
|
|
The second example is the test |
|
|
|
|
|
|
|
|
if (time1.LessThan(time2))
.
.
. |
|
|
|
|
|
|
|
|
where the programmer intends time1 to be 11:00:00 on a Wednesday and time2 to be 1:20:00 on a Thursday. (The result of the test is FALSE, not TRUE as the programmer expects.) Do you see the problem? In each example, the client has violated the function precondition. The precondition of Set requires the first parameter to have a value from 0 through 23. The precondition of LessThan requires the two times to be on the same day, not on two different days. |
|
|
|
|
|
|
|
|
If a class has been well tested and there are bugs when client code uses the class, always check the member function preconditions. You can waste many hours trying to debug a class member function when, in fact, the function is correct. The bug may lie in the client code. |
|
|
|
|
|
|
|
|
Testing and Debugging Hints |
|
|
|
|
|
|
|
|
1. The declarations of struct and class types both end with semicolons. |
|
|
|
|
|
|
|
|
2. Regarding semicolons, the declarations and definitions of class member functions are treated the same as any C++ function. The member function prototype, located in the class declaration, ends with a semicolon. The function headingthe part of the function definition preceding the bodydoes not end with a semicolon. |
|
|
|
|
|
|
|
|
3. When implementing a class member function, don't forget to prefix the function name with the name of the class and the scope resolution operator (::). |
|
|
|
|
|
|
|
|
void TimeType::Increment()
{
.
.
.
} |
|
|
|
|
|
|
|
|
4. For now, the only built-in operations that apply to class objects are member selection (.) and assignment (=). To perform other operations, such as comparing two class objects, you must write class member functions. |
|
|
|
|
|
|
|
|
5. If a class member function inspects but does not modify the private data, it is a good idea to make it a const member function. |
|
|
|
|
|