Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is the code: (main.cpp) #include #include #include vector.h using namespace std; int main() { /* Type your test code here. You will not be

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Here is the code:

(main.cpp)

#include #include #include "vector.h"

using namespace std;

int main() { /* Type your test code here. You will not be submitting this code. It's only for testing. */ return 0; }

(vector. h)

// vector class template

#ifndef Vector_Vector_h #define Vector_Vector_h

#include #include // Needed for bad_alloc exception #include // Needed for the exit function #include namespace CIST2362 {

template class vector { private: T *aptr; // To point to the allocated array unsigned long arraySize; // Number of elements in the array unsigned long arrayCapacity; // number of memory locations available to the vector

// increase capacity void increaseCapacity(unsigned long);

public: // Default constructor vector();

// Constructor declaration vector(unsigned long);

// Resize the vector - changes the size attribute void resize(unsigned long n);

// Resizes the vector and initializes the unused locations - updates the size attribute void resize(unsigned long n, const T &val);

// Copy constructor declaration vector(const vector &);

// Destructor declaration ~vector();

// Accessor to return the array size unsigned long size() const;

// Accessor to return the array capacity unsigned long capacity() const;

// Accessor to test empty status bool empty() const;

// Accessor to return a specific element T &at(unsigned long position);

// Overloaded [] operator declaration T &operator[](const unsigned long &);

// back element of the vector T &back();

// front element of the vector T &front();

void push_back(T); // New push_back member T pop_back(); // New pop_back member

// insert element at position int insert(unsigned long, const T &);

// erase a range of values void erase(unsigned long, unsigned long);

// erase one element at a position void erase(unsigned long); };

/* This is where your method definitions will be written. This default constructor * the first method you need to complete. To compile the program, you should create * empty stubs for each of the declared methods. */

template vector::vector() { }

} #endif

15.9 CIST2362 Programming Assignment: Building a Vector Class Replica Overview This programming assignment extends the idea that we had for SimpleVectorofInts to where you will create a replica of the std::vector class. The std: : vector class is what you have been using for a while now, and it is based on C++ Templates and is considered a container class. It will be helpful to consult the cplusplus.com reference for the std: : vector class as you work on this assignment. The left-navigation on this reference has the member functions. You can read the details about the member functions and there is even a way to try some sample test code using the std::vector class. This could be helpful as you try to replicate the behavior in your class. File Structure The main. cpp is to be used for testing. You can put in any code in there that you want. I suggest creating functions for your test as it makes it easier to run the tests that you want. Your vector class will be in the CIST2362 namespace. So it can be accessed as CIST2362: : vector when you are testing it. This helps deconflict the vector name from the std: : vector. For example, in your main. cpp, The vector. h is where your CIST2362: : vector class will be implemented. Because this is a template class, you will put all of the code in this .h file: class declaration (typically in the .h file) and method definitions (typically in the .cpp file). This is because the compiler needs access to the template class and method definitions in order to instantiate a class and methods of the type found in the . Requirements You will find the following attributes in the CIST2362:vector class: - T*aptr: The pointer being used for the internal array maintained by the vector class. You may change this name if you want :) - unsigned long arraySize: The value that represents the current number of elements in the vector - unsigned long arrayCapacity: The value that represents the current allocated memory capacity of the vector (must always be >= arraySize) You will find the following private method: - void increaseCapacity(unsigned long): This method is used for increasing the capacity of the vector. It should not be public, but other methods in the class would need it. throws the std: : bad_alloc ( ) exception if memory cannot be allocated. NOTE: You may add more private methods if you see opportunities for code reuse across the needs of the public vector methods. You will find the following public methods: There are 3 constructors. - vector(): This constructor creates a vector with a capacity of 10 and size of 0 . - vector(unsigned long): This constructor creates a vector with the capacity given by the parameter and size will be the same. - vector(const vector \&): This is the copy constructor for the class All constructors throw the std: : bad_alloc () exception if memory cannot be allocated. There are 2 resize methods: - void resize(unsigned long): Resize the vector based on this parameter. Resize can make it smaller or larger; if it's made smaller the capacity is unchanged (makes the vector more efficient). - void resize(unsigned long, const T \&): Resize the vector, and if it's made larger then fill in the new elements with value of the 2 nd parameter. The resize methods throw the std: : bad_alloc () exception if memory cannot be allocated The destructor: vector(): This needs to clean up all memory allocated to the vector Accessor methods: - unsigned long size() const: returns the size attribute of the vector - unsigned long capacity() const: returns the capacity attribute of the vector - bool empty ( const: returns true if the vector is empty, and false otherwise - T \&at(unsigned long): access the element at the given index and throws the std: : out_of_range exception if out of bounds - T \&operator[(const int \&): access the element at the given index and does not check bounds for efficiency purposes. - T \&back(): access the last element in the vector - T \& front(): access the first element in the vector Modifying methods: - void push_back( T) : adds an element to the end of the vector. If vector is full, increase capacity 50% of it's current capacity. - T pop_back(): removes the last element of the vector and returns it - int insert(unsigned long, const T \&): Inserts the element in the 2nd parameter at the position given by the 1st parameter - void erase(unsigned long, unsigned long): Removes the elements starting with the element at the position of the first parameter up to the element before the position in the 2nd parameter i.e. do not remove the element at the position of the 2nd parameter. - void erase(unsigned long): Removes the element at the given position Submission Submit the vector.h for testing each time you want to see how you are doing. Ensure that the tested methods are working before moving on. Realize that some of the tests will will multiple methods and so you may need to implement a few before you can pass some test. For example, you need a default constructor for every test, and there are many tests that require the [ ] operator

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

Step: 3

blur-text-image

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

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

More Books

Students also viewed these Databases questions

Question

assess the infl uence of national culture on the workplace

Answered: 1 week ago