|
|
|
|
|
|
|
In previous chapters, we have treated data structures as passive quantities to be acted upon by functions. In Chapter 12, we viewed an integer list as an ADT, representing the list as an int array and implementing the list operations as functions that take an int array as a parameter. Similarly, in Chapter 14, we treated an address book as an ADT, using a struct as the data representation and implementing the operations as functions receiving a struct as a parameter (see Figure 15-1). |
|
|
|
|
|
|
|
|
This separation of operations and data does not correspond very well with the notion of an abstract data type. After all, an ADT consists of both data values and operations on those values. It is preferable to view an ADT as defining an active data structureone that combines both data and operations into a single, cohesive unit (see Figure 15-2). C++ supports this view by providing a built-in structured type known as a class. |
|
|
|
|
|
|
|
|
In Chapter 14, we listed the four structured types available in the C++ language: the array, the struct, the union, and the class (Figure 14-5). A class is a structured type provided specifically for implementing abstract data types. A class is similar to a struct but is nearly always designed so that its components (class members) include not only data but also functions that manipulate that data. Here is a C++ class declaration corresponding to the TimeType ADT that we defined in the previous section: |
|
|
|
|
|
|
|
|
Figure 15-1
Data and Operations as Separate Entities |
|
|
|
|
|