Advertisement
prprice16

Untitled

Nov 18th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. //Set is a template that stores any datatype
  6. //all values must be unique
  7. template <typename T>
  8. class Set
  9. {
  10. private:
  11. //the datatype is T
  12. T* list; //a pointer for the array
  13. int size; //number of values stored
  14. public:
  15. Set(); //default constructor
  16. int getSize(); //return the number of elements
  17.  
  18. void add(T); //adds a value to list
  19. ~Set();
  20.  
  21. template <typename T>
  22. friend ostream& operator<<(ostream&, Set<T>);
  23.  
  24. void print();
  25. bool contains(T);
  26. void copy(const Set&);
  27. Set& operator=(const Set&);
  28. Set(const Set&); //copy constructor
  29. };
  30.  
  31. //FUNCTION DEFINITIONS
  32.  
  33. //Code default constructor
  34. template <typename T>
  35. Set<T>::Set()
  36. {
  37. //what code goes here?
  38. //list is initially empty
  39. size = 0;
  40. list = nullptr;
  41. }
  42.  
  43. template <typename T>
  44. int Set<T>::getSize()
  45. {
  46. //return the number of elements in the set
  47. return size;
  48. }
  49.  
  50. template <typename T>
  51. void Set<T>::add(T val)
  52. {
  53. //add val as the next element of list
  54. // ONLY add if val is not already in the list
  55. //need to allocate memory 1 bigger than current size
  56. if (!contains(val))
  57. {
  58. T* newlist = new T[size + 1];
  59.  
  60. //copy values from list to newlist
  61. for (int i = 0; i < size; i++)
  62. {
  63. //copy from list into newlist
  64. newlist[i] = list[i];
  65. }
  66.  
  67. //add val as last value
  68. newlist[size] = val;
  69.  
  70. //update size
  71. size++;
  72.  
  73.  
  74. //delete old list
  75. delete[] list;
  76.  
  77. //make newlist the list
  78. list = newlist;
  79. }
  80. /*
  81. cout << "in add" << endl;
  82. for (int i = 0; i < size; i++)
  83. {
  84. cout << list[i] << " ";
  85. }
  86. */
  87.  
  88. }
  89.  
  90. //destructor
  91. template <typename T>
  92. Set<T>::~Set()
  93. {
  94. //cout << "size is " << size << endl;
  95. //release memory associated with list
  96. delete[] list;
  97. //cout << "destructor called" << endl;
  98.  
  99. }
  100.  
  101. template <typename T>
  102. ostream& operator<<(ostream& out, Set<T> obj)
  103. {
  104. //cout << "printing size is " << obj.size << endl;
  105. //print data in obj
  106. //print each value separated by a space
  107. //data is in array called list
  108. for (int i = 0; i < obj.size; i++)
  109. {
  110. out << obj.list[i] << " ";
  111. }
  112. return out;
  113.  
  114. }
  115.  
  116.  
  117. template <typename T>
  118. void Set<T>::print()
  119. {
  120. //print all values in list
  121. for (int i = 0; i < size; i++)
  122. {
  123. cout << list[i] << " ";
  124. }
  125. }
  126.  
  127. template <typename T>
  128. bool Set<T>::contains(T val)
  129. {
  130. //return true if val is in the list
  131. //otherwise return false
  132. //search each element of list for val
  133. for (int i = 0; i < size; i++)
  134. {
  135. //did I find val?
  136. if (list[i] == val)
  137. return true;
  138. }
  139. //if you get here it was not found
  140. return false;
  141.  
  142.  
  143. }
  144.  
  145. template <typename T>
  146. void Set<T>::copy(const Set& right)
  147. {
  148. //make a copy of data in right
  149. size = right.size;
  150. //allocate memory for list
  151. list = new T[size];
  152. //copy the data over
  153. for (int i = 0; i < size; i++)
  154. {
  155. list[i] = right.list[i];
  156. }
  157. }
  158.  
  159.  
  160. template <typename T>
  161. Set<T>& Set<T>::operator=(const Set& right)
  162. {
  163. cout << "operator=" << endl;
  164. //make sure they are not the same object
  165. if (this != &right) //not the same address
  166. {
  167. //delete memory first
  168. delete[] list;
  169. //make the copy
  170. copy(right);
  171. }
  172. //return the object for chaining of =
  173. return *this;
  174. }
  175.  
  176. //copy constructor
  177. template <typename T>
  178. Set<T>::Set(const Set& right)
  179. {
  180. cout << "copy constructor" << endl;
  181. //is triggered when making a NEW object using
  182. //data from an existing object
  183. copy(right);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement