CS 10C Programming Concepts and Methodologies 2

Project 28.1

Implement a Set class (similar to the C++ set class) using a dynamic array. We will call the class "ArraySet".

You may want to review the section on sets from chapter 2. The most important attribute of a set is that it can never contain duplicate items.

(In the STL a set is required to store its elements in sorted order. We're not doing that here, so technically what we are writing is more like the unordered_set class in the STL.)

Below I have provided an "ArrayBag" class. The only difference between a "Bag" and a "Set" is that a Bag allows duplicate items and a Set does not. The vast majority of your code will be copied directly from this class. You just need to make minor adjustments, changing "Bag" to "Set" throughout the code, changing from a non-dynamic array to a dynamic array and reflecting the fact that duplicate elements are not allowed. (Note that you will receive 0 if your submitted code allows duplicate elements or uses a non-dynamic array, since these are pretty much the only things we are changing.)

You must provide the big-3. If you are not familiar with the term "big-3," please take a few minutes to review lesson 17.

Your ArraySet class will create an ArraySet with a capacity of DEFAULT_CAPACITY if the constructor is called with no arguments. If there is an int argument, an ArraySet with a capacity equal to that argument will be created. Do this by using default parameters, so you'll need just one constructor to handle these two situations. (You'll also need a copy constructor, for a total of two constructors.)

Note that once it is declared, the capacity of an ArraySet cannot be changed. If the insert() function is called when the capacity of the ArraySet has been reached, a CapacityExceededError will be thrown. This is already done for you in the ArrayBag version of insert().

To use a dynamic array in a class, you will declare just a pointer in the class declaration. Then, in the constructor, you will allocate the array.

Note that the count() function will always return 0 or 1, since duplicate items are not allowed.

The insert() function should throw a DuplicateItemError exception if the item passed in already exists in the Set.

Your ArraySet class must be derived from a "SetInterface" abstract class. (See below for more information about interface classes.) There's almost no work for you to do here. Just copy the "BagInterface" class below. None of the code in that abstract class will need to be modified other than changing "bag" to "set". Don't forget to change the namespace from cs_bag to cs_set.

Here is the source code that you are to use as a starting point.

For documentation, you must provide function documentation for any new functions that you are writing (just the big-3). No other documentation is required.

Important Submission Note

For this project and all future projects that involve templated classes, we will be combining the class declaration and class implementation into a single file for submission to zyBooks. I would suggest that you also do this during your development.

About Interface Classes

An interface class is a class whose purpose is to enforce a specification of a type, which can then be implemented using any structures the programmer chooses. As long as the implementation class is derived from the interface class, we can be assured that it will work as expected. As an example, here we are implementing the Set ADT using an array. Later in the course we will use the same interface class but implement the Set ADT using linked lists.