|
|
|
|
|
|
|
In Chapter 12, we introduced the notion of a list as an ADT. At various times, we have talked about typical list operations: insert an item into the list, delete an item, report whether the list is full, report whether the list is empty, search for a particular item, sort the list items into order, print the entire list, and so forth. Shouldn't we include all these operations when designing our list of time cards? Probably not. It's unlikely that a list of time cards would be considered general-purpose enough to warrant the effort. Let's stick to the operations we listed in the object table: |
|
|
|
|
|
|
|
|
Read all time cards into the list |
|
|
|
|
|
|
|
|
Look up time card information |
|
|
|
|
|
|
|
|
To choose a data representation for the list, let's review the relationships among the objects in our program. We said that the time card list object is composed of time card objects. Therefore, we can use a 500-element array of TimeCard class objects to represent the list. Because we'll be searching the array for time card information, the data representation should also include an integer variable indicating the length of the list (the number of array elements that are actually in use). |
|
|
|
|
|
|
|
|
Before we write the specification of the TimeCardList class, let's review the operations once more. The lookup operation must search the array for a particular time card. Because the array is potentially very large (500 elements), a binary search is better than a sequential search. However, a binary search requires the array elements to be in sorted order. We must either insert each time card into its proper place as it is read from the data file or sort the time cards after they have been read. Let's take the latter approach and add another operationa sorting operation. (We'll use the selection sort we developed in Chapter 12.) Finally, we need one more operation to initialize the private data: a class constructor. Here is the resulting class specification: |
|
|
|
|
|
|
|
|
//******************************************************************
// SPECIFICATION FILE (tclist.h)
// This file gives the specification of TimeCardList, an ADT for a
// list of TimeCard objects.
//******************************************************************
#ifndef TCLIST_H
#define TCLIST_H
#include timecard.h
#include bool.h
#include <fstream.h>
const int MAX_LENGTH = 500; // Maximum number of time cards
|
|
|
|
|
|