Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Dynamic Array Template Project Background We will add a few additional functions and turn DynArray into a class template so it can store any

C++ Dynamic Array Template Project

Background

We will add a few additional functions and turn DynArray into a class template so it can store any type of sequence. The DynArray code is included below

Objective:

In this project you will test your ability to write class and function templates.

HERE IS THE .h FILE FROM A PREVIOUS ASSIGNMENT (https://pastebin.com/1dLVf3KU) HERE IS THE .cpp DRIVER FILE ( https://pastebin.com/cd90LtFv )

Step One

Add the following new functions to the DynArray class in project #8.

1. The function int back().

This function returns the value of the last used integer in the vector, but it does not remove it. Throw a runtime_error if the vector is empty. This function returns by reference.

2. The function int front(). This function returns the value at the beginning the vector, but it does not remove it. Throw a runtime_error if the vector is empty. This function returns by reference.

3. The function operator[](int). This function overloads the [ ] operator we use to index into an array. If you need help understanding how to do this, review the chapter on operator overloading in Big C++. Note that this function returns an item by reference.

4. DynArray(const DynArray&). This is the copy constructor, which allocates new heap space for the new object it is copying from the object passed as its parameter.

5. DynArray& operator=(const DynArray&). This is the assignment opertator, which allocates new heap space for the new object it is copying from the object passed as its parameter. It also deletes its old heap space.

After you have added these functions to your DynArray class, write a driver that tests these new functions. Once you are satisfied that your DynArray class works, move on to step two.

Step Two

After adding the required code to your DynArray class, it is time to turn it into a class template. You will need to change any occurrences of the token "int" in your code that refer to an item in the vector to a template parameter (usually T). All of your code must appear in a header file (DynArray.h; you will need to move functions from DynArray.cpp in project #8 into this header file. You will have no DynArray.cpp).

To get you started implementing the member functions as templates, here is code implementing the capacity function template:

template int DynArray::capacity( ) const { return cap; }

Adding the Driver

To test your code, add the driver located here ( https://pastebin.com/cd90LtFv )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke

12th Edition International Edition

1292023422, 978-1292023427

More Books

Students also viewed these Databases questions

Question

=+3. Who are the brand's competitors?

Answered: 1 week ago