|
|
|
|
|
|
|
C++ refers to these functions as const member functions. Within the body of a const member function, a compile-time error occurs if any statement tries to modify a private data member. Although not required by the language, it is good practice to declare as const those member functions that do not modify private data. |
|
|
|
|
|
|
|
|
Specification and Implementation Files |
|
|
|
|
|
|
|
|
An abstract data type consists of two parts: a specification and an implementation. The specification describes the behavior of the data type without reference to its implementation. The implementation creates an abstraction barrier by hiding the concrete data representation as well as the code for the operations. |
|
|
|
|
|
|
|
|
The TimeType class declaration serves as the specification of TimeType. This declaration presents the public interface to the user in the form of function prototypes. To implement the TimeType class, we must provide function definitions (declarations with bodies) for all the member functions. |
|
|
|
|
|
|
|
|
In C++, it is customary (though not required) to package the class declaration and the class implementation into separate files. One filethe specification fileis a header (.h) file containing only the class declaration. The second filethe implementation filecontains the function definitions for the class member functions. Let's look first at the specification file. |
|
|
|
|
|
|
|
|
Below is the specification file for the TimeType class. On our computer system, we have named the file timetype.h. The class declaration is the same as we presented earlier, with one important exception: We include function preconditions and postconditions to specify the semantics of the member functions as unambiguously as possible for the user. |
|
|
|
|
|
|
|
|
//******************************************************************
// SPECIFICATION FILE (timetype.h)
// This file gives the specification
// of a TimeType abstract data type
//******************************************************************
#include bool.h
|
|
|
|
|
|