Question: Programming C++: In this assignment, we will use pointers to implement our own vector class. It will have the following features: Dynamically allocated array of

Programming C++:

In this assignment, we will use pointers to implement our own vector class. It will have the following features:

Dynamically allocated array of integers.

Size variable that keeps track of the number of elements in the array. Starts at 0.

Capacity variable that keeps track of the amount of memory allocated for the array. Starts at capacity 1.

Each time the size reaches the capacity, double the capacity

Several of the features available from the STL vector (insert, erase, push back, etc)

You will submit 3 files:

MyVector.h

MyVector.cpp

main.cpp

The code for the header file is as follows. It contains specifications and hints:

#ifndef MYVECTOR_H_INCLUDED #define MYVECTOR_H_INCLUDED class MyVector { public: /*Default constructor. Initialize size to 0, capacity to 1, and a to a new int array of size 1.*/ MyVector(); /*Return the array element at a given index*/ int at(int); /*Print all elements of the array*/ void printElements(); /*Return the number of elements*/ int getSize(); /*Return the allocated capacity of the array*/ int getCapacity(); /*Insert an element at a given index. The first parameter is the index, the second is the value to insert. if index == size, call pushBack. else if index > size || index < 0, output "Invalid index." else increase the size by 1, reallocate if size > capacity Create a loop that starts at the end of the array and decrements. Copy each element upward to make space for the new element. end the loop when you reach the insert index. when the loop is finished, assign value to the element at index*/ void insert(int, int); /*Erase an element at a given index. if index >= size || index < 0, output "Invalid index." else create a loop that starts at index and increments. copy each element downward until you reach size - 1. decrement the size and return.*/ void erase(int); /*Increment the size. If size > capacity, reallocate. assign the value to the end of the array.*/ void pushBack(int); private: /*Double the capacity. Since your capacity starts at 1, it will take on the values 1,2,4,8,16,... allocate a temp int array of size capacity to do this, assign "new int[capacity]" to your pointer copy a's values into temp with a loop deallocate a. To do this, use delete[] a. This prevents a memory leak. assign a to temp.*/ void reallocate(); int size; int capacity; int *a; }; #endif // MYVECTOR_H_INCLUDED 

Below are some sample outputs:

3 pushbacks:

Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 1 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 2 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 3 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 4 Contents: 1 2 3 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 7 

3 pushbacks and 3 inserts:

Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 1 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 3 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 5 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 2 Enter the number to insert: 2 Enter the index: 1 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 2 Enter the number to insert: 4 Enter the index: 3 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 4 Contents: 1 2 3 4 5 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 7 

5 pushbacks and 3 erases:

Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 1 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 2 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 3 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 4 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 1 Enter the number to push back: 5 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 3 Enter the index of the element to be removed: 2 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 3 Enter the index of the element to be removed: 3 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 3 Enter the index of the element to be removed: 0 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 4 Contents: 2 4 Choose from the following menu: 1) Push back 2) Insert 3) Erase 4) Print 5) Size 6) Capacity 7) Exit. 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!