Guest User

Untitled

a guest
Apr 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #ifndef INTCOLLECTION_H
  2. #define INTCOLLECTION_H
  3.  
  4. // Allocate memory in chunks of ints of this size.
  5. const int CHUNK_SIZE = 5;
  6.  
  7. class IntCollection
  8. {
  9. private:
  10. // The number of ints currently stored in the int
  11. int size;
  12. // the total number of elements available for storage
  13. // in the data array
  14. int capacity;
  15. // A pointer to the dynamically allocated data array
  16. int* data;
  17. // a private member function to allocate more memory
  18. // if necessary
  19. void addCapacity();
  20. public:
  21. // Constructor
  22. IntCollection();
  23. // Destructor
  24. ~IntCollection();
  25. // Copy constructor:
  26. IntCollection(const IntCollection &c);
  27.  
  28. void add(int value);
  29. int get(int index);
  30. int getSize();
  31. IntCollection& operator=(const IntCollection &c);
  32. bool operator==(const IntCollection &c);
  33. IntCollection& operator<<(int value);
  34. };
  35.  
  36. #endif
  37.  
  38. #include "IntCollection.h"
  39. #include <iostream>
  40. #include <cstdlib>
  41. using namespace std;
  42.  
  43. IntCollection::IntCollection()
  44. {
  45. // Initialize member data to reflect an empty
  46. // IntCollection
  47. size = capacity = 0;
  48. data = NULL;
  49. }
  50.  
  51. IntCollection::~IntCollection()
  52. {
  53. delete [] data;
  54. }
  55.  
  56. IntCollection::IntCollection(const IntCollection &c)
  57. {
  58. size = c.size;
  59. capacity = c.capacity;
  60. data = c.data;
  61.  
  62. for(int i = 0; i < c.size; i++)
  63. {
  64. data[i] = c.data[i];
  65. }
  66. }
  67.  
  68. void IntCollection::addCapacity()
  69. {
  70. // Create a new, bigger buffer, copy the current data to
  71. // it, delete the old buffer, and point our data
  72. // pointer to the new buffer
  73. int *newData;
  74. data = new int[capacity];
  75. capacity += CHUNK_SIZE;
  76. newData = new int[capacity];
  77.  
  78. for(int i = 0; i < size; i++)
  79. {
  80. newData[i] = data[i];
  81. delete [] data;
  82. data = newData;
  83. }
  84. }
  85.  
  86. void IntCollection::add(int value)
  87. {
  88. //first, allocate more memory if we need to
  89. if(size == capacity)
  90. {
  91. addCapacity();
  92. }
  93. //Now, add the data to our array and increment size
  94. data[size++] = value;
  95. }
  96.  
  97. int IntCollection::get(int index)
  98. {
  99. if (index < 0 || index >= size)
  100. {
  101. cout << "ERROR: get() trying to access index out of range.n";
  102. exit(1);
  103. }
  104. return data[index];
  105. }
  106.  
  107. int IntCollection::getSize()
  108. {
  109. return size;
  110. }
  111.  
  112. IntCollection& IntCollection::operator=(const IntCollection &c)
  113. {
  114. size = c.size;
  115. capacity = c.capacity;
  116. data = c.data;
  117.  
  118. return *this;
  119. }
  120.  
  121. bool IntCollection::operator==(const IntCollection &c)
  122. {
  123. if((size == c.size) && (capacity == c.capacity))
  124. {
  125. for(int m = 0; m < size; m++)
  126. {
  127. if(data[m] == c.data[m])
  128. {
  129. continue;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136. }
  137. return true;
  138. }
  139.  
  140. IntCollection& IntCollection::operator<<(int value)
  141. {
  142. return value; <-- THIS IS WHERE I GET LOST!
  143. }
Add Comment
Please, Sign In to add comment