[SOLVED] COP3503 - Lab 4 - Dynamic Array

30.00 $

Category:

Description

5/5 - (1 vote)

 Dynamic Array

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:

Program output: