Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- using namespace std;
- //Set is a template that stores any datatype
- //all values must be unique
- template <typename T>
- class Set
- {
- private:
- //the datatype is T
- T* list; //a pointer for the array
- int size; //number of values stored
- public:
- Set(); //default constructor
- int getSize(); //return the number of elements
- void add(T); //adds a value to list
- ~Set();
- template <typename T>
- friend ostream& operator<<(ostream&, Set<T>);
- void print();
- bool contains(T);
- void copy(const Set&);
- Set& operator=(const Set&);
- Set(const Set&); //copy constructor
- };
- //FUNCTION DEFINITIONS
- //Code default constructor
- template <typename T>
- Set<T>::Set()
- {
- //what code goes here?
- //list is initially empty
- size = 0;
- list = nullptr;
- }
- template <typename T>
- int Set<T>::getSize()
- {
- //return the number of elements in the set
- return size;
- }
- template <typename T>
- void Set<T>::add(T val)
- {
- //add val as the next element of list
- // ONLY add if val is not already in the list
- //need to allocate memory 1 bigger than current size
- if (!contains(val))
- {
- T* newlist = new T[size + 1];
- //copy values from list to newlist
- for (int i = 0; i < size; i++)
- {
- //copy from list into newlist
- newlist[i] = list[i];
- }
- //add val as last value
- newlist[size] = val;
- //update size
- size++;
- //delete old list
- delete[] list;
- //make newlist the list
- list = newlist;
- }
- /*
- cout << "in add" << endl;
- for (int i = 0; i < size; i++)
- {
- cout << list[i] << " ";
- }
- */
- }
- //destructor
- template <typename T>
- Set<T>::~Set()
- {
- //cout << "size is " << size << endl;
- //release memory associated with list
- delete[] list;
- //cout << "destructor called" << endl;
- }
- template <typename T>
- ostream& operator<<(ostream& out, Set<T> obj)
- {
- //cout << "printing size is " << obj.size << endl;
- //print data in obj
- //print each value separated by a space
- //data is in array called list
- for (int i = 0; i < obj.size; i++)
- {
- out << obj.list[i] << " ";
- }
- return out;
- }
- template <typename T>
- void Set<T>::print()
- {
- //print all values in list
- for (int i = 0; i < size; i++)
- {
- cout << list[i] << " ";
- }
- }
- template <typename T>
- bool Set<T>::contains(T val)
- {
- //return true if val is in the list
- //otherwise return false
- //search each element of list for val
- for (int i = 0; i < size; i++)
- {
- //did I find val?
- if (list[i] == val)
- return true;
- }
- //if you get here it was not found
- return false;
- }
- template <typename T>
- void Set<T>::copy(const Set& right)
- {
- //make a copy of data in right
- size = right.size;
- //allocate memory for list
- list = new T[size];
- //copy the data over
- for (int i = 0; i < size; i++)
- {
- list[i] = right.list[i];
- }
- }
- template <typename T>
- Set<T>& Set<T>::operator=(const Set& right)
- {
- cout << "operator=" << endl;
- //make sure they are not the same object
- if (this != &right) //not the same address
- {
- //delete memory first
- delete[] list;
- //make the copy
- copy(right);
- }
- //return the object for chaining of =
- return *this;
- }
- //copy constructor
- template <typename T>
- Set<T>::Set(const Set& right)
- {
- cout << "copy constructor" << endl;
- //is triggered when making a NEW object using
- //data from an existing object
- copy(right);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement