Lab 20: Implementing an ADT

Mar 28, 2024  β”‚  m. Mar 27, 2024 by Charlotte Curtis

Setup

This lab builds on the theoretical exercise from the previous lab . You will be implementing a portion of the SetInt class that you designed in the last lab.

To begin, git pull in your labs repo to get the new adt directory and cd into it.

Create a main.cpp file and #include "setint.h" at the top. You will be implementing the SetInt class in the setint.h and setint.cpp files.

In your main.cpp file, define the usual main function and declare some SetInt objects to test your implementation. While your main code is not tested in the lab, this is good practice for developing testing habits and thinking of how your class will be used, as well as good practice for the final exam.

Basic SetInt Implementation

In the last lab, you designed a SetInt class with a few basic operations and a private implementation. This time, I’ll be defining some of the public interface for you, but the implementation details are up to you! It doesn’t matter to the client code (my test code, or your main function) how you implement the class, as long as it behaves as expected.

Implement the following for your SetInt class, using your choice of private implementation:

  1. A default constructor that initializes an empty set.
  2. A destructor that frees any dynamically allocated memory.
  3. A contains member function that returns true if the set contains the given element, and false otherwise.
  4. An add member function to add an element to the set (making sure not to add duplicates - this is why contains is important!).
  1. A size member function that returns the number of elements in the set.

    This is the functionality that is tested by the provided test code. To run it, use the usual:

    $ make lab=adt
    

    from your top-level labs directory.

    As with the classes lab, the tested function depends on implementing the previous functions (constructor, add, and destructor) correctly. contains is not called directly by the test code, but you’ll probably need it to avoid adding duplicates in add.

Extending the SetInt Class

Once you have the basic functionality working, consider extending the functionality of your SetInt class. Here are some ideas:

  1. Overload the << operator to print the set to an output stream.
  2. Remove an element from the set.
  3. Implement the == operator to compare two sets for equality.
  4. Implement union and intersection operations for two sets.
  5. Query whether one set is a subset of another.


Previous: Lab 19: Designing an ADT
Next: Lab 21: Recursion