$25
Overview
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.
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<int> a; // Instance of Foo which uses ints
Foo<float> b; // Instance of Foo which uses floats
Foo<Bar> 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. It’s 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 basics—data 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 don’t NEED to (in your own projects, that is—you DO need to for this assignment), but it’s 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 wouldn’t normally have this in the final version of a class like this, but this can be helpful to illustrate what’s 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.
Exceptions
For functions which attempt to do something with a particular index, you will want to check to see if the indicated index is in a valid range. (Arrays have a valid range from 0 to arraySize-1.) If it isn't, you want to throw an exception--there are many ways you can handle exceptions, but in this assignment just throwing an exception of type runtime_error("Error! Invalid Index") will be acceptable. There is a section in your textbook which has examples of throwing and catching exceptions. You will also have to include the file <stdexcept>, and be sure you are using std::runtime_error.
Examples
A few examples of things you might want to do with an array:
Tips
A few tips about this assignment:
• Remember the "Big Three" or the “Rule of Three” o If you define one of the three special functions (copy constructor, assignment operator, or destructor), you should define the other two. The entire purpose of this class is based on the concept of dynamic memory allocation.
• Don't try to tackle everything all at once. Work on one function at a time. Be sure one thing works before moving on—that one thing could very well break a lot of other things later!
• Create your own tests! While the assignment submission on zyBooks has a lot of tests you will ultimately be graded on, you can/should be writing your own code to test functionality as you go. This also helps you to better understand your own code. Plus, it’s just good practice.
• Refer back to the recommended chapters in your textbook as well as lecture videos for an explanation of the details of dynamic memory allocation or templates o There are a lot of things to remember with memory allocation and deallocation
• If you think some of these tips seem familiar, you aren’t going crazy… it’s almost as if they are applicable to lots of situations in programming…
Program output: