6.8k views
5 votes
Lab: Vector Template C++

Background In Project 4 you built your own vector of ints. In this lab you will make your vector generic by using a
class template.
Requirements Make a copy of your Vector class from Project 4 and change all references to the type of element
contained in your array to be the template parameter T.

Once again, you are provided a test driver, testVector.cpp, and the test framework in test.h, to
rigorously test your project before you turn it in. The expected output is:
Test Report:

Number of Passes = 84
Number of Failures = 0

You can remove the trace print from the grow function for this lab. Once again, your grade will be the
percentage of tests that you pass.
Implementation Notes Remember that templates must all be defined inline within a single .h file


THIS IS PROJECT 4

#ifndef VECTOR_H
#define VECTOR_H
#include
#include
#include
using namespace std;
using std::size_t;

class Vector
{
enum
{
CHUNK = 10
};
int *data_ptr;
size_t capacity;
size_t n_elems;
void grow();

public:
Vector();
Vector(const Vector &v);
Vector &operator = (const Vector&v);
~Vector();

int front() const;
int back() const;
int at(size_t pos) const;
size_t size() const;
bool empty() const;

int &operator[](size_t pos);
void push_back(int item);
void pop_back();
void erase(size_t pos);
void insert(size_t pos, int item);
void clear();

int *begin();
int *end();

bool operator == (const Vector &v) const;
bool operator!=(const Vector &v)const;
};

Vector::Vector(){
data_ptr = new int[CHUNK];
capacity = CHUNK;
n_elems = 0;
}

Vector::Vector(const Vector &v){
this->capacity = v.capacity;
this->data_ptr = new int[this->capacity];
this->n_elems = v.n_elems;
for(int i = 0;ithis->data_ptr[i] = v.at(i);
}
}

Vector& Vector::operator=(const Vector &v){
this->capacity = v.capacity;
this->data_ptr = new int[this->capacity];
this->n_elems = v.n_elems;
for(int i = 0;ithis->data_ptr[i] = v.at(i);
}
return (*this);
}

Vector::~Vector(){
delete[] data_ptr;
}

int Vector::front() const{
if(this->n_elems==0){
throw range_error("Out of range");
}else{
return data_ptr[0];
}
}

int Vector::back() const{
if(this->n_elems==0){
throw range_error("Out of range");
}else{
return data_ptr[n_elems-1];
}
}

int Vector::at(size_t pos) const{
if(pos>= 0 && posreturn data_ptr[pos];
}else{
throw range_error("Out of bound");
}
}

size_t Vector::size() const{
return (size_t)n_elems;
}

bool Vector::empty() const{
return n_elems==0;
}

int& Vector::operator[](size_t pos){
return this->data_ptr[pos];
}

void Vector::push_back(int item){
if(capacity==n_elems){
capacity = (capacity*16)/10;
int* temp_ptr = new int[capacity];
for(int i = 0; itemp_ptr[i] = data_ptr[i];
}
delete[] data_ptr;
data_ptr = temp_ptr;
data_ptr[n_elems]=item;
n_elems++;
}else{
data_ptr[n_elems++] = item;
}
}

void Vector::pop_back(){
if(n_elems==0){
throw range_error("Out of bound");
}else{
n_elems--;
}
}

void Vector::erase(size_t pos){
if(pos>= 0 && posfor(int i=pos+1; idata_ptr[i-1] = data_ptr[i];
}
n_elems--;
}else{
throw range_error("Out of bound");
}
}

void Vector::insert(size_t pos, int item){
if(capacity == n_elems){
capacity = (capacity*16)/10;
int* temp_ptr = new int[capacity];
for(int i = 0; itemp_ptr[i] = data_ptr[i];
}
delete[] data_ptr;
data_ptr = temp_ptr;
}
for (int i=n_elems; i>pos; i--){
data_ptr[i] = data_ptr[i-1];
}
data_ptr[pos] = item;
n_elems++;
}

void Vector::clear(){
delete[] data_ptr;
data_ptr = new int[CHUNK];
capacity = CHUNK;
n_elems = 0;
}

//iterators
int* Vector::begin(){
if(n_elems == 0)return NULL;
return &data_ptr[0];
}

int* Vector::end(){
if(n_elems == 0)return NULL;
return &data_ptr[n_elems];
}

bool Vector::operator==(const Vector &v ) const{
if(this->capacity!=v.capacity)return false;
if(this->n_elems!=v.n_elems)return false;
for(int i=0;iif(this->data_ptr[i]!=v.data_ptr[i])return false;
}
return true;
}
bool Vector::operator!=(const Vector&v) const{
return !(operator ==(v));
}

#endif

User Ko Cour
by
8.0k points

1 Answer

2 votes

Final answer:

To transform your Vector class into a generic template, replace all 'int' types with 'T' and ensure all methods are included in the header file.

Step-by-step explanation:

The correct answer is to create a template class in C++ that will allow you to have a generic vector, rather than one specific to int types. To transform the provided Vector class into a templated version, you'll need to replace all instances where the data type int is specified with T, which will be the template parameter.

For example, you'll change the declaration from int *data_ptr to T *data_ptr, and likewise for all function parameters and return types that use int.

All method implementations will also need to be included within the header file, as template classes must be fully defined in each file they are used. This is because C++ templates are compiled when instantiated, and therefore their implementation always needs to be available to the compiler.

In this lab, the goal is to make the Vector class generic by using a class template called <T>. To achieve this, you need to replace all references to the type of elements (int) contained in the array with the template parameter T. This will allow the Vector class to hold elements of any type.

The implementation notes mention that all template definitions must be inline within a single .h file. This means that the template definition for the Vector class should be included in the Vector.h file.

Once you have made the necessary changes to the Vector class, you can use the provided test driver, testVector.cpp, and the test framework in test.h to rigorously test your project.

User Maurice Meyer
by
9.2k points