Advertisement
Guest User

dynamic_array C++ Class Definition

a guest
Nov 23rd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. template <typename T>
  2. class dynamic_array {
  3. public:
  4.     // Create an empty dynamic array.
  5.     dynamic_array() {
  6.         // Add your implementation here!
  7.         // (You may want to use an initializer list.)
  8.     }
  9.    
  10.     // Copy from another dynamic array instance.
  11.     dynamic_array(const dynamic_array<T>& array) {
  12.         // Add your implementation here!
  13.         // (You may want to use an initializer list.)
  14.     }
  15.    
  16.     // Assign from another dynamic array instance.
  17.     dynamic_array<T>& operator=(const dynamic_array<T>& array) {
  18.         // Add your implementation here!
  19.     }
  20.    
  21.     // Destroy this array instance.
  22.     ~dynamic_array() {
  23.         // Add your implementation here!
  24.     }
  25.    
  26.     // Get the current array size.
  27.     size_t size() const {
  28.         // Add your implementation here!
  29.     }
  30.    
  31.     // Access an element by index.
  32.     T& operator[](size_t index) {
  33.         // Add your implementation here!
  34.     }
  35.    
  36.     // Access an element by index (const overload).
  37.     const T& operator[](size_t index) const {
  38.         // Add your implementation here!
  39.     }
  40.    
  41.     // Resize the array to contain 'newSize' elements.
  42.     // If the new array size is larger fill the new slots with copies of 'value'.
  43.     void resize(size_t newSize, const T& value = T()) {
  44.         // Add your implementation here!
  45.     }
  46.    
  47.     // Append element to back of array.
  48.     void push_back(const T& element) {
  49.         // Add your implementation here!
  50.     }
  51.    
  52.     // Remove last element.
  53.     void pop_back() {
  54.         // Add your implementation here!
  55.     }
  56.  
  57. private:
  58.     // Add your member variables here!
  59.    
  60. };
  61.  
  62. // This should work with the following use case:
  63. void arrayExample() {
  64.     dynamic_array<int> array;
  65.    
  66.     // array = {}
  67.    
  68.     array.push_back(1);
  69.    
  70.     // array = { 1 }
  71.    
  72.     array.push_back(2);
  73.    
  74.     // array = { 1, 2 }
  75.    
  76.     array.pop_back();
  77.    
  78.     // array = { 1 }
  79.    
  80.     array.push_back(4);
  81.    
  82.     // array = { 1, 4 }
  83.    
  84.     array.resize(4, 100);
  85.    
  86.     // array = { 1, 4, 100, 100 }
  87.    
  88.     // Prints array contents.
  89.     for (size_t i = 0; i < array.size(); i++) {
  90.         printf("array[%u] = %d\n", (unsigned) i, array[i]);
  91.     }
  92.    
  93.     // Call copy constructor for dynamic_array.
  94.     dynamic_array<int> arrayCopy = array;
  95.    
  96.     // array = { 1, 4, 100, 100 }, arrayCopy = { 1, 4, 100, 100 }
  97.    
  98.     arrayCopy.pop_back();
  99.    
  100.     // array = { 1, 4, 100, 100 }, arrayCopy = { 1, 4, 100 }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement