< previous page page_894 next page >

Page 894
6. A member function does not use dot notation to access private members of the class object for which the function is invoked. In contrast, a member function must use dot notation to access the private members of a class object that is passed to it as a parameter.
7. To avoid bugs caused by uninitialized data, it is good practice to always include a class constructor when designing a class.
8. A class constructor is declared without a return value type and cannot return a function value.
9. It is not possible to pass parameters to a class constructor when creating an array of class objects. The class either must have no constructors at all or must include a default (parameterless) constructor, which is then invoked for each element of the array.
10. If a client of a class has bugs that seem to be related to the class, start by checking the preconditions of the class member functions. The errors may be in the client, not the class.
Summary
Data abstraction is a powerful technique for reducing the complexity and increasing the reliability of programs. Separating the properties of a data type from the details of its implementation frees the user of the type from having to write code that depends on a particular implementation of the type. This separation also assures the implementor of the type that client code cannot accidentally compromise a correct implementation.
An abstract data type (ADT) is a type whose specification is separate from its implementation. The specification announces the abstract properties of the type. The implementation consists of (a) a concrete data representation and (b) the implementations of the ADT operations. In C++, an ADT can be realized by using the class mechanism. A class is similar to a struct, but the members of a class are not only data but also functions. Class members can be designated as public or private. Most commonly, the private members are the concrete data representation of the ADT, and the public members are the functions corresponding to the ADT operations.
Among the public member functions of a class, the programmer often includes one or more class constructorsfunctions that are invoked automatically whenever a class object is created.
Separate compilation of program units is central to the separation of specification from implementation. The declaration of a C++ class is typically placed in a specification (.h) file, and the implementations of the class member functions reside in another file: the implementation file. The client code is compiled separately from the class implementation file, and the two resulting object code files are linked together to form an executable file. Through separate compilation, the user of an ADT can treat the ADT as an off-the-shelf component without ever seeing how it is implemented.

 
< previous page page_894 next page >