// Computer Science 367, Section 3, Fall 1997 // Instructor: Michael Siff (siff@cs.wisc.edu) // // Programming Assignment One: Solution // // // FILE: set.h // // // TEMPLATE CLASS PROVIDED: Set (a container template class // for a set of ElementTypes) // // The template parameter, ElementType, is the data type of the items in // the Set. It may be any of the C++ built-in types (int, char, etc.), or // a class with a default constructor, a copy constructor, an assignment // operator, and operators to test for equality (x==y) and inequality(x!=y). // // // CONSTRUCTORS for the Set template class: // Set() // Postcondition: The set has been initialized as an empty set. // // Set(const ElementType& elem) // Postcondition: The set has been initialized as a singleton set // consisting of a copy of elem. // // Set(const Set& source) // Postcondition: The set has been initialized as a copy of the // source. (This is the copy constructor.) // // // DESTRUCTOR for the Set template class: // ~Set() // Postcondition: The memory allocated to store the set has been freed. // // // MODIFICATION MEMBER FUNCTIONS for the Set template class: // void add(const ElementType& elem) // Precondition: The set is not full. // Postcondition: A copy of elem is a member of the set. If the set // contained elem prior to the add operation then the set remains the // same. // // void remove(const ElementType& elem) // Postcondition: elem is not a member of the set. If elem was in the // set then it has been removed. If it wasn't then the set remains the // same. // // void operator=(const Set& source) // Postcondition: The set that activated this function is a copy of // source. // // void reset() // Postcondition: The set is reset to the empty set. // // // CONSTANT MEMBER FUNCTIONS for the Set template class: // size_t size() const // Postcondition: The return value is the number of elements in the set. // // bool isEmpty() const // Postcondition: True if the set is empty, false otherwise. // // bool isFull() const // Postcondition: True if the set is full, false otherwise. // // bool isMember(const ElementType& elem ) const // Postcondition: True if elem is in the set, false otherwise. // // bool operator==(const Set& operand) const // Postcondition: True if this set and the set operand have equivalent // contents - each item present in one set is present in the other set. // // Set operator+(const Set& otherSet) const // Postcondition: The return value is a new set that is the union of // this set and otherSet. If the new set does not have the capacity to // hold all the necessary elements, then the new set is empty. // // Set intersection(const Set& otherSet) const // Postcondition: The return value is a new set that is the // intersection of this set and otherSet. // // Set operator-(const Set& otherSet) const // Postcondition: The return value is a new set that is the // difference of this set and otherSet. That is, the new set consists // of all the elements of this set except those in otherSet. // // ElementType *toArray() const // Postcondition: The return value is a dynamic allocated array // consisting of one copy of each of the elements in the set. #ifndef SET_TEMPLATE_H #define SET_TEMPLATE_H #include // Provides size_t #include "list.h" // Provides list template template class Set { public: // CONSTRUCTORS Set(); Set(const ElementType& elem); Set(const Set& originalSet); // copy constructor // DESTRUCTOR ~Set(); // MODIFICATION MEMBER FUNCTIONS void add(const ElementType& elem); void remove(const ElementType& elem); void operator=(const Set& source); void reset(); // CONSTANT MEMBER FUNCTIONS size_t size() const; bool isEmpty() const; bool isFull() const; bool isMember(const ElementType& elem ) const; bool operator==(const Set& operand) const; Set operator+(const Set& otherSet) const; Set intersection(const Set& otherSet) const; Set operator-(const Set& otherSet) const; ElementType *toArray() const; private: List rep; // The set is really stored as a list }; #include "set.C" // include the template implementation #endif