Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could someone help me write the .h and .cpp file for these. I am having a hard t

ID: 674446 • Letter: C

Question

Could someone help me write the .h and .cpp file for these. I am having a hard time writing the declearations in the .h and and the methods in the .cpp . I would be grateful for anyone who could help Thank you!.

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 class.cpp file

Vector::Vector(std::initializer_list<int> list)
:element{ new int[list.size()] }, sz{ list.size() }
{
copy(list.begin(), list.end(), element);
}

Vector::Vector(int s)
{
if (s < 0) throw length_error{"Vector::Vector"};
element = new int[s];
for (int i = 0; i < s; i++)
element[i] = 0;
sp = s;
}

Vector::Vector(const Vector& a)
(if destructor then copy constructor & copy assignment op)
:element{ new int[sz] },
sp{ a.sz }
{
for (int i = 0; i < sp; i++)
elem[i] = a.element[i];
}

Vector::Vector()
{
sp = 0;
}

Vector::~Vector()
{
cout << "DESTRUCTOR TRIGGERED! END OF DAYS COMING ";
delete[] element;
}

int& Vector::operator[](int i) const
{
if (i<0 || i>=size()) throw out_of_range{ "Vector::operator[]" };
return element[i];
}

Vector& Vector::operator=(const Vector& a)
{
int* p = new int[a.sp];
for (int i = 0; i < sp; i++)
p[i] = a.element[i];
delete[] element;
this->element = p;
this->sp = a.sp;
return *this;
}


const bool Vector::operator==(Vector& right) const
{
if (size() != right.size())
return false;
else {
for (int i = 0; i < size(); i++)
{
size, doesn't matter which
if (elem[i] != right[i])
return false;
}
}
return true;
}


Vector& Vector::operator+=(const Vector& a)
{
int* p = new int[sp + a.sp];
for (int i = 0; i < sp; i++)
p[i] = element[i];
for (int i = sz, ctr = 0; i < sp + a.sp; i++, ctr++)
p[i] = a.element[ctr];
delete[] elem;
this->element = p;
this->sp += a.sp;
return *this;
}

const Vector& Vector::operator++() {
this->pushBack(0);
return *this;
}

const Vector& Vector::operator--() {
  


this->sp -= 1;
return *this;
}

const Vector Vector::operator+(const Vector& a) {

if (this->sp != a.sp)
return NULL;
Vector v = sp;
for (int i = 0; i < this->sp; i++)
v.element[i] = element[i] + a.element[i];
return v;
}

const Vector& Vector::operator+(int x) {
this->pushBack(x);
return *this;
}


Vector Vector::softRest() const
{
Vector v = *this;
int* p = new int[v.sp - 1];
for (int i = 0; i < sp - 1; i++)
p[i] = v[i + 1];
delete[] v.elem;
v.elem = p;
v.sp -= 1;
return v;
}


const Vector& Vector::hardRest() {
int* p = new int[sp - 1];
for (int i = 0; i < sp - 1; i++)
p[i] = element[i + 1];
delete[] element;
this->element = p;
this->sz -= 1;
return *this;
}


const Vector& Vector::pushBack(int x) {
int* temp = new int[sp + 1];
for (int i = 0; i < sp; i++)
temp[i] = element[i];
temp[sp] = x;
temp[sp] = last element
delete[] element;
this->element = temp;
this->sp += 1;
return *this;
}


void Vector::addToEnd(std::initializer_list<int> list)
{
Vector v(list);
this->operator+=(v);
}

int Vector::size() const {
return this->sp;
}

vector.h

#include <initializer_list>
#include <iostream>
#include <stdexcept>
using namespace std;

class Vectors {
public:
  
Vectors(std::initializer_list<int>);
init (ie Vectors v({1, 2, 3}); )
Vectors(int);

Vectors();   

Vectors(const Vectors& a);

~Vectors();


  
int& operator[](int) const;
a[i] (or &a[i])
const Vectors& operator++();
const Vectors& operator--();
const bool operator==(Vectors&) const;
Vectors& operator=(const Vectors&);
Vectosr& operator+=(const Vectors&);
const Vectors operator+(const Vectors&);
const Vectors& operator+(int); int (essentially .pushBack(int))

  
const Vectors& pushBack(int);
void addToEnd(std::initializer_list<int>);
Vectors softRest() const;
const Vectors& hardRest();

  
int size() const;
void arr_print() const;


private:
int sp;
int* element;
};