I NEED HELP TO WRITE MyVector.h and MyVector.cpp USING THE INSTRUCTION BELOW For
ID: 3880236 • Letter: I
Question
I NEED HELP TO WRITE MyVector.h and MyVector.cpp USING THE INSTRUCTION BELOW
For this assignment you will add a copy constructor and the following overloaded operators to your MyVector class: <, ==, =, +, <<, [], and ++.
Here's a description of how each operator should behave:
operator< :
returns true if the length of the left-hand side MyVector is less than the length of the right-hand side MyVector, false otherwise.
operator== :
returns true if both MyVectors contain the exact same values, false otherwise.
operator= :
overwrites the contents of the left-hand side MyVector with the contents of the right-hand side MyVector.
operator+ :
returns a MyVector object that contains an element-by-element sum of the two operators. Consider the following snippet:
MyVector m, n;
cout << m + n;
If m contains the values 1, 2, 3 and n contains the values 4, 5, then the output would be:
[5, 7, 3].
operator<< :
sends a string representation of the MyVector object to the output stream. For example, if the MyVector contains 8 6 7, then operator<< would return [8, 6, 7] so that if you had a statement like
MyVector m;
...
cout << m;
You would see
[8, 6, 7]
on the screen.
operator++:
post-fix version. increments each element by 1. Returns a MyVector object.
operator[]:
allows for retrieving and changing elements of the MyVector.
AND
GIVEN THE main.cpp TO TEST THE PROGRAM.
#include "MyVector·h" #include iostream» using namespace std; #include.c stdlib int main() MyVector m for(int i = 0; iExplanation / Answer
//Copyable Code:
MyVector.h
========
#include <iostream>
using namespace std;
class MyVector {
private:
int* list;
int max_size;
int list_size;
public:
// Constructor
MyVector() {
max_size = 10;
list_size = 0;
list = new int[max_size];
}
//copy constructor
MyVector(const MyVector &v2)
{
max_size = 10;
list_size = v2.list_size;
list = new int[max_size];
for(int i = 0; i < list_size; i++)
list[i] = v2.list[i];
}
MyVector& operator = (const MyVector &v2)
{
list_size = v2.list_size;
for(int i = 0; i < list_size; i++)
list[i] = v2.list[i];
return *this;
}
bool operator < (const MyVector &v2)
{
return list_size < v2.list_size;
}
bool operator == (const MyVector &v2)
{
if(list_size == v2.list_size)
{
for(int i= 0; i < list_size; i++)
{
if(list[i] != v2.list[i])
return false;
}
return true;
}
else
return false;
}
MyVector operator +(const MyVector &v2)
{
MyVector result;
int sz;
//find out how many elements the reuslt will have
if(list_size > v2.list_size)
sz = list_size;
else
sz = v2.list_size;
result.list_size = sz;
int n1, n2;
for(int i = 0; i < sz; i++)
{
//get the ith element from thsi list
if(i < list_size)
n1 = list[i];
else
n1 = 0;
//get the ith element from parameter sent
if(i < v2.list_size)
n2 = v2.list[i];
else
n2 = 0;
result.list[i] = n1+n2;
}
return result;
}
friend ostream& operator << (ostream& out, const MyVector &v2)
{
cout << "[";
if(v2.list_size > 0)
{
cout << v2.list[0] ;
for(int i = 1; i < v2.list_size; i++)
cout << ", " << v2.list[i];
}
cout << "]";
return out;
}
int operator[](int index) const
{
return list[index];
}
int& operator[](int index)
{
return list[index];
}
MyVector operator++(int ) //postfix increment
{
MyVector copy(*this); //save the before values to be returned
for(int i = 0; i < list_size; i++)
list[i]++;
return copy;
}
// Destructor
~MyVector() {
delete[]list;
}
void push_back(int i)
{
if(list_size < max_size)
{
list[list_size++] = i;
}
}
void pop_back()
{
if(list_size > 0)
{
list_size--;
}
}
int& at(int i)
{
return list[i];
}
void clear()
{
list_size = 0;
}
int size()
{
return list_size;
}
};
======================================================
Test driver
#include "MyVector.h"
using namespace std;
int main()
{
MyVector m;
for(int i = 0; i < 5; i++)
m.push_back(1 + rand() % 100);
cout << m.size() << endl;
cout << m << endl;
MyVector n(m);
cout << n << endl;
n++;
cout << n << endl;
MyVector o;
o = m + n;
cout << o << endl;
cout << (o < m) << endl;
cout << (n == m) << endl;
}
======================================================
//Sample Output:
5
[8, 50, 74, 59, 31]
[8, 50, 74, 59, 31]
[9, 51, 75, 60, 32]
[17, 101, 149, 119, 63]
0
0
Feel free to reach out if you have any doubts.
Rate if the answer was helpful.
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.