As your specifi cations become more detailed, they increasingly should refl ect your choice of pro- gramming language. Ultimately, you can write C++ headers for the bag’s methods and organize them into a header fi le for the class that will implement the ADT.
The C++ header fi le in Listing 1-1 contains the methods for an ADT bag and detailed comments that describe their behaviors. If you are unfamiliar with C++ classes, templates, and virtual methods, C++ Interlude 1 provides a refresher on C++ classes and discusses C++ templates, virtual methods, and abstract base classes. By using templates and abstract base classes, we can take advantage of the object-oriented design concepts of encapsulation, polymorphism, and inheritance beginning with our fi rst ADT.
Note:
Dealing with unusual conditionsAs mentioned earlier, you must make decisions about how to treat unusual conditions. The documentation for the ADT bag should refl ect both these decisions and the details in the previous discussion. For example, our specifi cation of the method remove indicates that it will return false if a client tries to remove an entry from an empty bag. Instead, we could have given the method the precondition that the bag not be empty. It is then up to the client to check that the precondition is satisfi ed before invoking the method. Notice that the client can use other methods of the ADT bag, such as isEmpty and getCurrentSize , to help with this task.
Note:
A fi rst draft of an ADT’s specifi cations often overlooks or ignores situations that you really need to consider. You might intentionally make these omissions to simplify this fi rst draft. Once you have written the major portions of the specifi cations, you can con- centrate on the details that make the specifi cations complete.Question 1
Write specifi cations using UML notation for a function that computes the sum of the fi rst fi ve positive integers in an array of n arbitrary integers.CHECK POINT
Note:
To emphasize the distinction between the design of an ADT and its implementa- tion with a data structure, this textbook uses templates and abstract base classes in the description of our ADTs. We will call the header fi le containing the abstract base class aclient interface .2 A programmer should not need anything more than the client interface
to use a class in your program, as it completely specifi es the methods of the ADT.
2 If you are familiar with Java, the interfaces for our ADTs correspond to Java interfaces. Both provide a public interface consisting of methods that the programmer of our ADT must implement.
As you examine the interface in Listing 1-1, notice the decisions that were made to address the unusual situations mentioned in the previous section. In particular, each of the methods add , remove , andcontains returns a value. Note the commenting tags, which begin with @ and are described in Appendix I , that we use to specify the methods.
LISTING 1-1 A fi le containing a C++ interface for bags
/** @file BagInterface.h */ #ifndef _BAG_INTERFACE #define _BAG_INTERFACE #include <vector>
template <class ItemType> class BagInterface {
public :
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
virtual int getCurrentSize() const = 0; /** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
virtual bool isEmpty() const = 0; /** Adds a new entry to this bag.
@post If successful, newEntry is stored in the bag and the count of items in the bag has increased by 1. @param newEntry The object to be added as a new entry. @return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this bag, if possible.
@post If successful, anEntry has been removed from the bag and the count of items in the bag has decreased by 1. @param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType& anEntry) = 0; /** Removes all entries from this bag.
@post Bag contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Counts the number of times a given entry appears in bag. @param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
The ADT Bag 23
For now, the items in the bag will be objects of the same class. To accommodate entries of any single class type, the bag methods use a generic type ItemType for each entry. To give meaning to the identifi er ItemType , we must write template<class ItemType> on the line before the class header. Once the actual data type is chosen by a client, the compiler will use that data type wherever ItemType appears.
The class BagInterface is a C++ abstract base class. An abstract base class , or simply an
abstract class , in C++ contains at least one method that is declared as virtual and has no implementa-
tion. An abstract class cannot be instantiated; it can only be used as a base class. The subclass must then implement the methods specifi ed but not defi ned in the abstract class.
/** Tests whether this bag contains a given entry. @param anEntry The entry to locate.
@return True if bag contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const= 0; /** Empties and then f ills a given vector with all entries that are in this bag.
@return A vector containing all the entries in the bag. */
virtual vector<ItemType> toVector() const = 0; }; // end BagInterface