Question: In this assignment you are going to create a class called DynamicArray. The purpose of this class is to store an array that will make
In this assignment you are going to create a class called DynamicArray. The purpose of this class is to store an array that will make use of the C++ concept of templates to store any type of data. In addition, you will write functions to expand or shrink the array as needed.
Description:
All programs need storage of some kind as they execute. Arrays are the most basic, and probably the most common form of storage. However, because of their fixed nature they can be problematic to use, particularly if you need to store data of varying lengths as your applications needs change over time. One of the first data structures people start to use in any programming language is some form of an expandable array. Java has the ArrayList class, C# has the List class, and even C++ has the vector class.
The DynamicArray class you create will be very similar to the vector class. The concepts behind allocating and freeing memory will be applicable when implementing numerous other data structures and algorithms, so you arent reinventing the wheel here. Youre learning how wheels are made, so you can build your own, custom version of them as needed in later projects.
Templates:
Templates in C++ can be a bit confusing at first, but they allow you to reuse code very easily. By defining a class as a template, you can create multiple instances of that class to use different types. For example:
Foo a; // Instance of Foo which uses ints
Foo b; // Instance of Foo which uses floats
Foo c; // Instance of Foo which uses Bars
The DynamicArray class you write is going to be a storage container, and while you may create storage containers in the future that are custom-built to solve a single problem, reusable code can make your life much easier.
DynamicArray
Storage
Internally, a class like this does not store much data. There are only three data members that you are required to store (though you may add any additional variables you see fit to help you write this).
Data - A pointer to the data you are storing. This will be based on the type-id you define for the template. This pointer is the core of this class. Its where the magic happens.
Size How many objects are being stored currently?
Capacity How many objects COULD be stored? This is not the same as the size. Think of a 2-car garage. Its capacity is 2, but at any given point it may have 0, 1, or 2 cars parked in it.
Functions
A class like this is all about its functionality. Basic arrays do absolutely nothing. A class like this becomes much more useful when it can DO something. First, the basicsdata access. Then, the mutators. Last, but not least, constructors and the Big Three.
Why use a function AND an overloaded operator to do the same thing? Truthfully, you dont NEED to (in your own projects, that isyou DO need to for this assignment), but its good experience to see that there is more than one way to accomplish something in code.
The Resize() function should print out the old capacity, as well as the new capacity. You wouldnt normally have this in the final version of a class like this, but this can be helpful to illustrate whats happening. You can print this with a line such as this: cout << "Resizing... old capacity: " << (TheOldCapacity) << " New capacity: " << (TheNewCapacity) << endl; And of course, the Big Three, or Trilogy of Evil, whichever you prefer. Plus, a pair of constructors. They might be evil. They might not be. Hard to say. But keep an eye on them, just in case.
Main.cpp:
#include
// Sample class, to show a template can work with anything class Hero { string name_; int hitpoints_; int maxHP_; public: Hero() {} Hero(const char *name, int hp, int maxHP) { name_ = name; hitpoints_ = hp; maxHP_ = maxHP; } void SetName(const char *name) { name_ = name; } string GetName() { return name_; } friend ostream &operator<<(ostream &os, const Hero &h) { cout << h.name_ << " Hitpoints: " << h.hitpoints_ << " / " << h.maxHP_; return os; } };
void TestOne(); void TestTwo(); void TestThree(); void TestFour();
int main() { cout << "**** Testing the Dynamic Array ****" << endl << endl; TestOne(); TestTwo(); TestThree(); TestFour(); return 0; }
void TestOne() { cout << "Integer container: Initial capacity of 0" << endl; DynamicArray
cout << "Capacity: " << data.GetCapacity() << endl; cout << "Size: " << data.GetSize() << endl; } void TestTwo() { DynamicArray
for (int i = 0; i < 5; i++) data2.Add((float)101 / (i + 2));
for (unsigned int i = 0; i < data2.GetSize(); i++) cout << data2[i] << endl;
cout << "Capacity: " << data2.GetCapacity() << endl; cout << "Size: " << data2.GetSize() << endl;
cout << " Shrinking container to 2 elements..." << endl; data2.Resize(2);
for (unsigned int i = 0; i < data2.GetSize(); i++) cout << data2[i] << endl; cout << "Capacity: " << data2.GetCapacity() << endl; cout << "Size: " << data2.GetSize() << endl;
cout << "Attempting to access index[50]..."; try { cout << data2[50] << endl; } catch (runtime_error &msg) { cout << msg.what() << endl; } } void TestThree() { DynamicArray
data3.Add(100); data3.Add(999); data3.Add(200); data3.Add(300); data3.Add(400);
for (unsigned int i = 0; i < data3.GetSize(); i++) cout << data3[i] << endl;
cout << "Removing '999' from the list... " << endl; data3.Remove(1);
for (unsigned int i = 0; i < data3.GetSize(); i++) cout << data3[i] << endl;
cout << "Creating a copy of the list (testing the copy constructor)... " << endl; auto data4 = data3;
cout << " Printing copy of dynarray..." << endl; for (unsigned int i = 0; i < data4.GetSize(); i++) cout << data4[i] << endl; cout << " Attempting to remove index[100]..." << endl; try { data3.Remove(100); } catch (runtime_error &msg) { cout << msg.what() << endl; }
} void TestFour() { DynamicArray
for (unsigned int i = 0; i < heroes.GetSize(); i++) cout << heroes[i] << endl;
DynamicArray
cout << " Printing cloned heroes from dynarray (and changing their names)..." << endl; for (unsigned int i = 0; i < heroClones.GetSize(); i++) { string name = "Evil " + heroClones[i].GetName(); heroClones[i].SetName(name.c_str()); cout << heroClones[i] << endl; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
