Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. /***************************************************************
  2. Author: Dr. Daniel Spiegel, Updated by: Trisha Badlu
  3. Creation Date: 21 March 2017
  4. Due Date: 31 March 2017
  5. Assignment: #3
  6. Filename: Array.cpp
  7. Course: CSC136 - 020
  8. Professor Name: Dr. Spiegel
  9. Purpose: Define member functions for Array class.
  10. ***************************************************************/
  11. // File: Array.cpp
  12. // Member function definitions for class Array
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <stdlib.h>
  16. #include <assert.h>
  17. #include "Array.h"
  18. #include "SortSearch.h"
  19.  
  20. using namespace std;
  21.  
  22. // Initialize static data member at file scope
  23. int Array::arrayCount = 0; // no objects yet
  24.  
  25. // Default constructor for class Array (default size 10)
  26. Array::Array(int arraySize){
  27. setEltsInUse(0);
  28. setCapacity(( arraySize > 0 ? arraySize : 10 ));
  29. ptr = new int[getCapacity()]; // create space for array
  30. assert( ptr != 0 ); // terminate if memory not allocated
  31. ++arrayCount; // count one more object
  32.  
  33. for ( int index = 0; index < getCapacity(); index++ )
  34. ptr[ index ] = 0; // initialize array
  35. }
  36.  
  37. // Copy constructor for class Array
  38. // must receive a reference to prevent infinite recursion
  39. Array::Array(const Array &init){
  40. setEltsInUse(init.getEltsInUse());
  41. setCapacity(init.getCapacity());
  42. ptr = new int[getCapacity()]; // create space for array
  43. assert( ptr != 0 ); // terminate if memory not allocated
  44. ++arrayCount; // count one more object
  45.  
  46. for ( int index = 0; index < getEltsInUse(); index++ )
  47. ptr[ index ] = init.ptr[ index ]; // copy init into object
  48. }
  49.  
  50. // Destructor for class Array
  51. Array::~Array(){
  52. delete [] ptr; // reclaim space for array
  53. --arrayCount; // one fewer objects
  54. }
  55.  
  56. // Set the Array's size
  57. void Array::setCapacity(int elts){
  58. capacity=elts;
  59. }
  60.  
  61. // Get the size of the array
  62. int Array::getCapacity() const{
  63. return capacity;
  64. }
  65. // Get the amount of elements in use
  66. int Array::getEltsInUse() const{
  67. return numElts;
  68. }
  69. // Set the amount of elements in use
  70. void Array::setEltsInUse(int eltsInUse){
  71. numElts = eltsInUse;
  72. }
  73. // Sorts the elements in the array
  74. Array &Array::sort(){
  75. selSort(ptr, getEltsInUse());
  76. }
  77.  
  78. // Overloaded assignment operator
  79. // const return avoids: ( a1 = a2 ) = a3
  80. const Array &Array::operator=( const Array &right ){
  81. if ( &right != this ) { // check for self-assignment
  82.  
  83. // for arrays of different sizes, deallocate original
  84. // left side array, then allocate new left side array.
  85. if ( getCapacity() != right.getCapacity() ) {
  86. delete [] ptr; // reclaim space
  87. setCapacity(right.getCapacity()); // resize this object
  88. setEltsInUse(right.getEltsInUse());
  89. ptr = new int[getCapacity()]; // create space for array copy
  90. assert( ptr != 0 ); // terminate if not allocated
  91. }
  92.  
  93. for ( int index = 0; index < getEltsInUse(); index++ )
  94. ptr[ index ] = right[ index ]; // copy array into object
  95. }
  96.  
  97. return *this; // enables x = y = z;
  98. }
  99. // Determine if two arrays are equal and
  100. // return true, otherwise return false.
  101. bool Array::operator==(const Array &right) const{
  102. if ( getEltsInUse() != right.getEltsInUse() )
  103. return false; // arrays of different sizes
  104.  
  105. for ( int index = 0; index < getEltsInUse(); index++ )
  106. if ( ptr[ index ] != right[ index ] )
  107. return false; // arrays are not equal
  108.  
  109. return true; // arrays are equal
  110. }
  111.  
  112. // Determine if two arrays are not equal and
  113. // return true, otherwise return false (uses operator==).
  114. bool Array::operator!=(const Array &right) const{
  115. return ! ( *this == right );
  116. }
  117.  
  118. // Added a specified number to the array and incrementing numElts
  119. // Creates new ptr if ptr is already full, also increasing capacity
  120. const Array &Array::operator+=(const int right){
  121. if(getEltsInUse() == getCapacity()){
  122. setCapacity(getCapacity()+1);
  123. int *tempArray = new int[getCapacity()];
  124. for(int index = 0; index < getCapacity() - 1; index++)
  125. tempArray[index] = ptr[index];
  126. tempArray[getCapacity()-1] = right;
  127. delete [] ptr;
  128. ptr = tempArray;
  129. }
  130. else
  131. ptr[getEltsInUse()] = right;
  132. setEltsInUse(getEltsInUse()+1);
  133.  
  134. return *this;
  135. }
  136.  
  137. // Overloaded subscript operator for non-const Arrays
  138. // reference return creates an lvalue
  139. int &Array::operator[](int subscript){
  140. // check for subscript out of range error
  141. assert( 0 <= subscript && subscript < getEltsInUse() );
  142. return ptr[subscript]; // reference return
  143. }
  144.  
  145. // Overloaded subscript operator for const Arrays
  146. // const reference return creates an rvalue
  147. const int &Array::operator[](int subscript) const{
  148. // check for subscript out of range error
  149. assert( 0 <= subscript && subscript < getEltsInUse() );
  150.  
  151. return ptr[ subscript ]; // const reference return
  152. }
  153.  
  154. // Return the number of Array objects instantiated
  155. // static functions cannot be const
  156. int Array::getArrayCount(){
  157. return arrayCount;
  158. }
  159.  
  160. // Overloaded output operator for class Array
  161. ostream &operator<<(ostream &output, const Array &right){
  162. int index;
  163. //output << '\t';
  164. for ( index = 0; index < right.getEltsInUse(); index++ ) {
  165. output << setw( 12 ) << right[ index ];
  166.  
  167. if ( ( index + 1 ) % 4 == 0 ) // 4 numbers per row of output
  168. output << endl;
  169. }
  170.  
  171. if ( index % 4 != 0 )
  172. output << endl;
  173.  
  174. return output; // enables cout << x << y;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement