Lab Eight Solution

$30.00 $24.90

Description

Objectives:

The objective of this lab is to increase your understanding of how a vector is implemented, and get you started on the design of your next project. In your next project you will implement a vector-of-integers class.

After completing this lab, you should be able to

Explain how a vector works.

Develop a design for a vector-of-integers class

Study Material

Implementing a Vector class

Programming Assignment

For this lab, read the study material provided. Then design a vector-of-integers class. Your class must contain at least the following functions. Be sure to use the function names as described here or your code will not work with the driver supplied in project #8:

A default constructor that creates an vector with a default capacity of 2

A parameterized constructor that creates a vector of capacity n

A destructor that deletes any dynamically allocated storage. The destructor has the same name as the class, but begins with a ~. The destructor has no return type and takes no parameters, for example ~MyVector( )

Note that you program should not have any memory leaks.

A function, size( ), that returns the size of your vector.

A function, capacity( ), that returns the capacity of the vector.

A function, clear( ), that deletes all of the elements from the vector and resets its size to zero and its capacity to two.

A function push_back(int n) that adds the integer value n to the end of the vector. If the vector is not large enough to hold this additional value, you must make the vector grow. Your grow algorithm should double the current capacity of the vector. Don’t forget to consider the case where the initial capacity of the vector is zero.

A function at(int n) that returns the value of the element at position n in the vector. If the index n is greater than the size( ) of the vector, this function should throw an exception.

When you are satisfied with your design, create a header file for your vector-of-integers class.

File(s) to Submit:

A .h file for the class that you have designed. No class diagram is required.

Grading

Your lab will be graded on the quality of your design.

Description

Points possible

Your points

Assignment meets the submission guidelines.

o All source code files contain a complete file prologue

o Source code files contain declaration that you did not copy code

o Project has been properly submitted to Canvas

o Code meets style guidelines

2

Your .h file meets the specifications of the problem.

3

Total

5