Homework 8

Template Classes and Exceptions - MyVector

The purpose of this asssignment is to create a template class for a dynamically sized array. This will be similar to the C++ vector class from the standard template library (STL).

You will have two files in total: MyVector.cpp, and hw8.cpp ←(given).

You will be creating a template class called MyVector. This class will support a partially filled dynamic array of any datatype. Since it is a partially filled dynmic array, it will have three member variables: a pointer for the array (template pointer), an integer for the current count of elements, and an iteger for the array's capacity. The capacity will represent the total amount of allocated index the array may contain. The count will represent the current amount of elements within the array and the value will always be less than or equal to the capacity.

In addition, you will also create two empty exception classes:

MyVector

Main function

Use the following menu program for your main function. This is a nested menu which means you will have two loops. The outer loop will print the main menu while the inner loop will print the sub-menu that was selected in the outer loop. When the program is started, the main menu is presented. The sub-menu will only be shown when the user selects that sub-menu off the main menu.

To implement the nested menus, have the main menu in the main() function. Each option in the main menu will call another function which will display and process the sub-menu.
The main menu appears as follows:

    Welcome to the CS2020 Homework 8 Menu
==============================================
1.  Test the MyVector class for integers
2.  Test the MyVector class for doubles
3.  Test the MyVector class for characters

0.  Exit the program
==============================================

MyVector Sub-Menu given:

            MyVector Sub-Menu 
========================================================================
1.  Print the capacity and size of the list
2.  Clear all the elements on the list
3.  Add an element to the end of the list
4.  Remove the last element from the list and print its value 
5.  Use the index operator to set and print the value of an element
6.  Use the reserve function to change the number of elements allocated 
7.  Use the resize function to change the number of elements in use
8.  Search for a given index in the list
9.  Print the current contents of the list

0.  Return to the main menu
========================================================================

Each menu option is testing a member function or operator in the MyVector class. Option 1 will print the results of capacity() and size(). Option 2 will call the clear() function. Option 3 will call the push_back() function. Option 4 calls the pop_back() function. Option 5 uses the index operator. Option 6 uses the reserve() function. Option 7 uses the resize() function. Option 8 uses the find() function. Option 9 uses the output operator.

When the user gives option 0, return back to the main menu by exitting this function. This will switch back to main()'s scope. As long as your main menu loop is coded correctly, this should trigger the main menu being printed again so the user can test another MyVector object.

For options 1, 2, 4, 8 and 9, if the list is currently empty (empty() returns true), print out an error message about the list being empty so the operation could not be completed. Otherwise, perform the indicated operation.

For options 3, 6 and 7, be sure to use a try/catch block to check for the OutOfMemory exception being thrown. If there is an allocation failure, do NOT exit the program. Instead, return back to the main menu by exitting the sub-menu function.

For option 3, prompt the user for the new element and then use the push_back() function to add the element to the list.

For option 5, use a try/catch block to check for an invalid index being given. You can use the logic in main() for Lab 8 as inspiration for how to implement this option. Once the user gives a valid index, print the current value of the element at that index to the screen then ask the user for the new value of the element at that index. Read in the user's response and set the element at that index to the new value.

For option 6, prompt the user for the new capacity of the MyVector and then pass that to the reserve() function. For option 7, prompt the user for the new count of the MyVector and then pass that to the resize() function.

For option 8, prompt the user for the element they wish to search for. Use the find() function in the MyVector object. If find returns -1, tell the user that the element was not found. Otherwise, tell the user that the element was found at the returned index.


Have your completed source code in the required directory, otherwise I will not be able to extract your homework. It is important that the directory and filename are exact.
~/2020_S19/wk8/hw8.cpp
~/2020_S19/wk8/MyVector.cpp