I will be forever grateful if someone could show me how to write the methods to
ID: 674147 • Letter: I
Question
I will be forever grateful if someone could show me how to write the methods to this. I am having such a hard time figuring it out!
class Vector
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, a data member is also maintained inside the class to determine the maximum number of elements that may be stored in the array (the vector capacity). Another data member is used to keep track of the number of data items currently stored in the vector (the vector size). Both of these data members should be declared as data type size_t (which corresponds to an size_teger).
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 nullptr. Alternately, the data members can be initialized in the class declaration, in which case this method's body can be empty.
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& other)
The class should also have a proper copy constructor. Your code should account for the possibility that you might be copying an empty Vector object.
Vector& Vector::operator=(const Vector& rhs)
The assignment operator should be properly overloaded to allow one Vector object to be assigned to another. Your code should account for the possibility that you might be copying an empty Vector object.
ostream& operator<<(ostream& lhs, const Vector& rhs)
The output operator should be overloaded so that a Vector can be printed on the standard output. As in Assignment 4, this will need to be a standalone friend rather than a method.
The items stored in the vector should be printed starting with the front item and ending with the back item.
void Vector::clear()
This method should set the vector size to 0. It should not change the vector capacity or the vector array.
size_t Vector::size() const
This method should return the vector size.
size_t 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(size_t 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:
Set the vector capacity to the new capacity.
Declare a local pointer to an int and use it to allocate a new temporary array of integers. The number of elements for this new array should be equal to the vector capacity.
Loop through the vector array from subscript 0 up to the vector size, copying elements from the vector array to the temporary array.
Delete the vector array.
Assign the temporary array pointer to the vector array pointer.
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:
If the two Vector objects do not have the same vector size, they can not be equal and the method should return false.
Otherwise, the method should loop through the vector arrays of the two objects from 0 up to the vector size. If an element of the left operand's vector array does not equal the corresponding element of the right operand's vector array, return false. Otherwise, simply continue the loop.
If the loop finishes without having returned false, all the elements of both vector arrays are equal and the method should return true.
Explanation / Answer
This is technique for managing dynamically-allocated storage in C++.
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
int main()
{
cout << "Checking default constructor ";
Vector vec1;
cout << "vec1: " << vec1 << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "vec1 capacity: " << vec1.capacity() << endl;
cout << "vec1 is " << ((vec1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking push_back() ";
for (int i = 10; i < 200; i+= 10)
vec1.push_back(i);
cout << "vec1: " << vec1 << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "vec1 capacity: " << vec1.capacity() << endl;
cout << "vec1 is " << ((vec1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking pop_back() ";
for (int i = 0; i < 3; ++i)
{
vec1.pop_back();
}
cout << "vec1: " << vec1 << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "vec1 capacity: " << vec1.capacity() << endl;
cout << "vec1 is " << ((vec1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking copy constructor() ";
Vector vec2 = vec1;
cout << "vec1: " << vec1 << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "vec1 capacity: " << vec1.capacity() << endl;
cout << "vec1 is " << ((vec1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "vec2: " << vec2 << endl;
cout << "vec2 size: " << vec2.size() << endl;
cout << "vec2 capacity: " << vec2.capacity() << endl;
cout << "vec2 is " << ((vec2.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking front() and back() ";
cout << "Front item of vec1: " << vec1.front() << endl;
cout << "Front item of vec2: " << vec2.front() << endl << endl;
cout << "Rear item of vec1: " << vec1.back() << endl;
cout << "Rear item of vec2: " << vec2.back() << endl << endl;
cout << "vec1: " << vec1 << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "vec1 capacity: " << vec1.capacity() << endl;
cout << "vec1 is " << ((vec1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "vec2: " << vec2 << endl;
cout << "vec2 size: " << vec2.size() << endl;
cout << "vec2 capacity: " << vec2.capacity() << endl;
cout << "vec2 is " << ((vec2.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking pop() to empty ";
while (!vec1.empty())
{
cout << vec1.back() << ' ';
vec1.pop_back();
}
cout << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "vec1 capacity: " << vec1.capacity() << endl;
cout << "vec1 is " << ((vec1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking assignment operator ";
Vector vec3;
vec3 = vec2;
cout << "vec2: " << vec2 << endl;
cout << "vec2 size: " << vec2.size() << endl;
cout << "vec2 capacity: " << vec2.capacity() << endl;
cout << "vec2 is " << ((vec2.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "vec3: " << vec3 << endl;
cout << "vec3 size: " << vec3.size() << endl;
cout << "vec3 capacity: " << vec3.capacity() << endl;
cout << "vec3 is " << ((vec3.empty()) ? "empty " : "not empty ");
cout << endl;
Vector vec4;
vec3 = vec4;
cout << "vec3: " << vec3 << endl;
cout << "vec3 size: " << vec3.size() << endl;
cout << "vec3 capacity: " << vec3.capacity() << endl;
cout << "vec3 is " << ((vec3.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "vec4: " << vec4 << endl;
cout << "vec4 size: " << vec4.size() << endl;
cout << "vec4 capacity: " << vec4.capacity() << endl;
cout << "vec4 is " << ((vec4.empty()) ? "empty " : "not empty ");
cout << endl;
vec3 = vec2;
cout << "vec3: " << vec3 << endl;
cout << "vec3 size: " << vec3.size() << endl;
cout << "vec3 capacity: " << vec3.capacity() << endl;
cout << "vec3 is " << ((vec3.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking clear() ";
vec2.clear();
cout << "vec2 (size " << vec2.size() << "): " << vec2 << endl;
cout << "vec3 (size " << vec3.size() << "): " << vec3 << endl << endl;
cout << "Checking assignment to self and swap ";
vec3 = vec3;
vec2 = vec3;
vec3.clear();
cout << "vec2 (size " << vec2.size() << "): " << vec2 << endl;
cout << "vec3 (size " << vec3.size() << "): " << vec3 << endl << endl;
cout << "Checking chained assignment ";
Vector vec5;
vec5 = vec3 = vec2;
cout << "vec2 (size " << vec2.size() << "): " << vec2 << endl;
cout << "vec3 (size " << vec3.size() << "): " << vec3 << endl;
cout << "vec5 (size " << vec5.size() << "): " << vec5 << endl << endl;
cout << "Checking write version of subscript operator ";
for (size_t i = 0; i < vec5.size(); ++i)
vec5[i] += 5;
cout << "vec5 (size " << vec5.size() << "): " << vec5 << endl << endl;
cout << "Checking read version of subscript operator vec5: ";
for (size_t i = 0; i < vec5.size(); ++i)
cout << vec5[i] << ' ';
cout << endl << endl;
cout << "Checking const correctness ";
const Vector& r5 = vec5;
cout << "vec5: " << r5 << endl;
cout << "vec5 size: " << r5.size() << endl;
cout << "vec5 capacity: " << r5.capacity() << endl;
cout << "vec5 is " << ((r5.empty()) ? "empty " : "not empty ");
cout << "Front item of vec5: " << r5.front() << endl;
cout << "Rear item of vec5: " << r5.back() << endl;
cout << "4th item of vec5: " << r5[3] << endl << endl;
vec1 = r5;
cout << "vec1: " << vec1 << endl;
cout << "vec1 size: " << vec1.size() << endl;
cout << "vec1 is " << ((vec1.empty()) ? "empty " : "not empty ");
cout << endl;
cout << "Checking equality operator ";
cout << "vec1 and vec5 are " << ((vec1 == r5) ? "equal " : "not equal ");
cout << "vec5 and vec3 are " << ((r5 == vec3) ? "equal " : "not equal ");
cout << "vec1 and vec2 are " << ((vec1 == vec2) ? "equal " : "not equal ");
vec5.pop_back();
cout << "vec1 and vec5 are now " << ((vec1 == vec5) ? "equal " : "not equal ");
cout << endl;
return 0;
}
NOTE