In this assignment, you will write a class called Vector that will encapsulate a
ID: 3544673 • Letter: I
Question
In this assignment, you will write a class called Vector that will encapsulate a dynamically-allocated array of integers.
A driver program is provided for this assignment to test your implementation. You don't have to write the tests.
You will need to write a single class for this assignment, the Vector class. You will need to implement several methods and functions associated with this class.
The class should be implemented as two separate files. The class definition should be placed in an appropriately named header (.h) file. The implementations of the class methods and any other associated functions should be placed in a separate .cpp file for the class.
Data members
This class contains a pointer to an integer that will point to the first element of a dynamically allocated array of integers (the vector array). Because the array is allocated dynamically, an integer value is also maintained inside the class to determine the maximum number of elements that may be stored in the array (the vector capacity). Another integer variable is used to keep track of the number of data items currently stored in the vector (the vector size).
Methods and associated functions
Vector::Vector()
The class should have a default constructor that takes no arguments. The constructor should set the vector size and vector capacity to 0 and the vector array to NULL.
Vector::~Vector()
The class should have a destructor that deletes the dynamic memory for the vector array. The destructor should NOT call the clear() method.
Vector::Vector(const Vector& vectorToCopy)
The class should also have a proper copy constructor.
const Vector& Vector::operator=(const Vector& rightOp)
The assignment operator should be properly overloaded to allow one Vector object to be assigned to another.
ostream& operator<<(ostream& leftOp, const Vector& rightOp)
The output operator should be overloaded so that a Vector can be printed on the standard output. As in Assignments 4 and 5, this will need to be a standalone friend rather than a method.
The items stored in the vector should be printed starting with the first array element (subscript 0) and ending with the last valid element (subscript vector size - 1).
void Vector::clear()
This method should set the vector size to 0. It should not change the vector capacity or the vector array.
int Vector::size() const
This method should return the vector size.
int Vector::capacity() const
This method should return the vector capacity.
bool Vector::empty() const
This method should return true if the vector size is equal to 0; otherwise it should return false.
int Vector::front() const
This method should return the first element of the vector array (the one at the subscript 0). You may assume this method will not be called if the vector is empty.
int Vector::back() const
This method should return the last valid element of the vector array (the one at the subscript (vector size - 1). You may assume this method will not be called if the vector is empty.
void Vector::push_back(int item)
This method takes an integer argument, the item to insert into the vector. If the vector is full (the vector size is equal to the vector capacity), this method will need to call the reserve() method to increase the capacity of the vector array and make room for the item to insert. If the vector capacity is currently 0, pass a new capacity of 1 to the reserve() method. Otherwise, pass a new capacity of twice the current vector capacity to the reserve() method..
Using the vector size as the subscript, copy the item to be inserted into the vector array. The vector size should then be incremented by 1.
void Vector::pop_back()
This method should decrement the vector size by 1, which effectively removes the last item from the vector array. No changes need be made to the vector array contents. You may assume this method will not be called if the vector is empty.
void Vector::reserve(int newCapacity)
This method increases the capacity of the vector array. It takes a single integer argument, the new capacity. The logic for this method should look something like this:
int Vector::operator[](int sub) const
This method should return the element of the vector array at subscript sub. You may assume this method will not be called if the vector is empty.
int& Vector::operator[](int sub)
This method should return the element of the vector array at subscript sub. You may assume this method will not be called if the vector is empty.
bool Vector::operator==(const Vector& rightOp) const
This method is used to compare two Vector objects. The method should return true if the Vector objects are equal; otherwise, it should return false. The logic for this method should look something like this:
Explanation / Answer
//Vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
using namespace std;
class Vector
{
public:
Vector();
~Vector();
Vector(const Vector&);
const Vector& operator=(const Vector&);
friend ostream& operator<<(ostream&, const Vector&);
void clear();
int size() const;
int capacity() const;
bool empty() const;
int front() const;
int back() const;
void push_back(int);
void pop_back();
void reserve(int);
int operator[](int) const;
int& operator[](int);
bool operator==(const Vector&) const;
private:
int* arr;
int cap;
int sz;
};
#endif // VECTOR_H
//Vector.cpp
#include "Vector.h"
Vector::Vector() : arr(NULL), cap(0), sz(0) { }
Vector::~Vector() { delete [] arr; }
Vector::Vector(const Vector& other)
: arr(NULL), cap(other.cap), sz(other.sz)
{
arr = new int[cap];
for (int i = 0; i < sz; ++i) arr[i] = other.arr[i];
}
const Vector& Vector::operator=(const Vector& rhs)
{
if (this == &rhs) return *this;
delete [] arr;
cap = rhs.cap;
sz = rhs.sz;
arr = new int[cap];
for (int i = 0; i < sz; ++i) arr[i] = rhs.arr[i];
return *this;
}
ostream& operator<<(ostream& lhs, const Vector& rhs)
{
lhs << '[';
if (rhs.sz) lhs << rhs.arr[0];
for (int i = 1; i < rhs.sz; ++i) lhs << ", " << rhs.arr[i];
return lhs << ']';
}
void Vector::clear() { sz = 0; }
int Vector::size() const { return sz; }
int Vector::capacity() const { return cap; }
bool Vector::empty() const { return sz == 0; }
int Vector::front() const { return arr[0]; }
int Vector::back() const { return arr[sz-1]; }
void Vector::push_back(int item)
{
if (!cap) reserve(1);
if (sz == cap) reserve(cap *= 2);
arr[sz++] = item;
}
void Vector::pop_back() { --sz; }
void Vector::reserve(int newCapacity)
{
cap = newCapacity;
int* newArr = new int[cap];
for (int i = 0; i < sz; ++i) newArr[i] = arr[i];
delete [] arr;
arr = newArr;
}
int Vector::operator[](int sub) const { return arr[sub]; }
int& Vector::operator[](int sub) { return arr[sub]; }
bool Vector::operator==(const Vector& rhs) const
{
if (sz != rhs.sz) return false;
for (int i = 0; i < sz; ++i)
if (arr[i] != rhs.arr[i]) return false;
return true;
}