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:
- A default constructor that initializes an empty set.
- A destructor that frees any dynamically allocated memory.
- A
contains
member function that returnstrue
if the set contains the given element, andfalse
otherwise. - An
add
member function to add an element to the set (making sure not to add duplicates - this is whycontains
is important!).
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 inadd
.
Extending the SetInt
Class
Once you have the basic functionality working, consider extending the functionality of your SetInt
class. Here are some ideas:
- Overload the
<<
operator to print the set to an output stream. - Remove an element from the set.
- Implement the
==
operator to compare two sets for equality. - Implement union and intersection operations for two sets.
- Query whether one set is a subset of another.