Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- using namespace std;
- template <typename T>
- class DynamicArray
- {
- private:
- int size; //make sure to change this as needed
- T * array;
- public:
- DynamicArray(); //sets size to zero, array to nullptr
- DynamicArray(DynamicArray const &a);
- int getSize() const;
- void addEntry(const T); //Adds an element at the end of the array
- T getEntry(int) const; //Returns the element at that index
- bool deleteEntry(T); //Deletes an element. if nothing is deleted, the function returns false
- ~DynamicArray();
- };
- template <typename T>
- DynamicArray<T>:: DynamicArray()
- {
- size = 0;
- array = new T[size];
- }
- template <typename T>
- DynamicArray<T>:: DynamicArray (const DynamicArray<T> &a) //copy constructor
- {
- size = a.getSize();
- T *DynamicArray = new T[size];
- for (int i = 0; i < size; i++) //Copy over the elements
- {
- array[i] = a.getEntry(i);
- }
- }
- template <typename T>
- DynamicArray<T>:: ~DynamicArray() //Deletes the array and sets it's pointer to nullptr when the object is deleted
- {
- delete [] array;
- }
- template <typename T>
- int DynamicArray<T>:: getSize() const
- {
- return size;
- }
- template <typename T>
- void DynamicArray<T>:: addEntry(T const var)
- {
- T *newArray = new T[size+1];
- //Copy contents from old array into the new array
- for (int i = 0; i < size; i++)
- {
- newArray[i] = array[i];
- }
- newArray[size] = var; //initiallize the last element with the argument
- size++; //resizing
- delete[] array; //delete/clear the old array
- array = newArray; //Setting to the new array
- delete [] newArray; //Free memory
- }
- template <typename T>
- T DynamicArray<T>:: getEntry(int index) const
- {
- if (index >= size || index < 0) //Check if index in argument is also an existing index in the array
- {
- return 0;
- }
- else
- {
- return array[index];
- }
- }
- template <typename T>
- bool DynamicArray<T>:: deleteEntry(T var)
- {
- for (int i = 0; i < size; ++i)
- {
- if (array[i] ==var)
- {
- //replace the var, and move everything down
- for (int j = i; j < size-1; ++j)
- {
- array[j] = array[(j+1)];
- }
- T *newArray = new T[size-1];
- //Copy over the old array into the new array
- //We do this because we want an array that doesn't have an "empty" index at the end
- for(int j = 0; j < size-1; ++j)
- {
- newArray[j] = array[j];
- }
- array = newArray;
- delete [] newArray;
- newArray = nullptr;
- //decrease size since an element was taken out
- size--;
- return true;
- }
- }
- return false;
- }
- int main()
- {
- DynamicArray<string> names;
- // List of names
- names.addEntry("Frank");
- names.addEntry("Wiggum");
- names.addEntry("Nahasapeemapetilon");
- names.addEntry("Quimby");
- names.addEntry("Flanders");
- // Output list
- cout << "List of names:" << endl;
- for (int i = 0; i < names.getSize(); i++)
- cout << names.getEntry(i) << endl;
- cout << endl;
- // Add and remove some names
- names.addEntry("Spuckler");
- cout << "After adding a name:" << endl;
- for (int i = 0; i < names.getSize(); i++)
- cout << names.getEntry(i) << endl;
- cout << endl;
- names.deleteEntry("Nahasapeemapetilon");
- cout << "After removing a name:" << endl;
- for (int i = 0; i < names.getSize(); i++)
- cout << names.getEntry(i) << endl;
- cout << endl;
- names.deleteEntry("Skinner");
- cout << "After removing a name that isn't on the list:" << endl;
- for (int i = 0; i < names.getSize(); i++)
- cout << names.getEntry(i) << endl;
- cout << endl;
- names.addEntry("Muntz");
- cout << "After adding another name:" << endl;
- for (int i = 0; i < names.getSize(); i++)
- cout << names.getEntry(i) << endl;
- cout << endl;
- // Remove all of the names by repeatedly deleting the last one
- while (names.getSize() > 0) {
- names.deleteEntry(names.getEntry(names.getSize() - 1));
- }
- cout << "After removing all of the names:" << endl;
- for (int i = 0; i < names.getSize(); i++)
- cout << names.getEntry(i) << endl;
- cout << endl;
- names.addEntry("Olivia");
- cout << "After adding a name:" << endl;
- for (int i = 0; i < names.getSize(); i++)
- cout << names.getEntry(i) << endl;
- cout << endl;
- cout << "Testing copy constructor" << endl;
- DynamicArray<string> names2(names);
- // Remove Olivia from names
- names.deleteEntry("Olivia");
- cout << "Copied names:" << endl;
- for (int i = 0; i < names2.getSize(); i++)
- cout << names2.getEntry(i) << endl;
- cout << endl;
- cout << "Testing assignment" << endl;
- DynamicArray<string> names3 = names2;
- // Remove Olivia from names2
- names2.deleteEntry("Olivia");
- cout << "Copied names:" << endl;
- for (int i = 0; i < names3.getSize(); i++)
- cout << names3.getEntry(i) << endl;
- cout << endl;
- cout << "Testing dynamic array of ints" << endl;
- DynamicArray<int> nums;
- nums.addEntry(10);
- nums.addEntry(20);
- nums.addEntry(30);
- for (int i = 0; i < nums.getSize(); i++)
- cout << nums.getEntry(i) << endl;
- cout << endl;
- cout << "Enter a character to exit." << endl;
- char wait;
- cin >> wait;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement