Review the entire code. Define a copy method void copy(const vector* other) and add it/incorporate it into the vector class definition (i.e. the file you will submit should have everything our vector class has + new method copy). The copy method is given a pointer to the other vector object and should copy all the information from that vector to this vector. Note that you cannot guarantee that this and the other vector are the exact same sizes.
#include <iostream>
class vector {
int sz;
double * elem;
public:
vector(int s): sz(s), elem(new double[s]) {
for (int i{0}; i < s; ++i) elem[i] = 0;
}
~vector() {
delete[] elem;
}
double get(int n) const {return elem[n];};
void set(int n, double v) {elem[n] = v;}
int size() const {return sz;}
void display() {
for (int i{ 0 }; i < sz; ++i)
std::cout << elem[i] << " ";
}
void resize(int newSz) {
double* tmp = new double[newSz];
for (int i{0}; i < sz; ++i)
tmp[i] = elem[i];
for (int i{sz}; i < newSz; ++i)
tmp[i] = 0;
delete[] elem;
elem = tmp;
sz = newSz;
}
};
std::ostream & operator<<(std::ostream & out, const vector & v) {
for (int i{0}; i < v.size(); ++i)
out << v.get(i) << " ";
return out;
}
int main() {
vector a(10);
for(int i{0}; i<10; ++i)
a.set(i, 2 * i + 1);
std::cout << a << std::endl;
a.resize(20);
std::cout << "\nThe element of the vector after resizing: \n" << a << std::endl;
}