I need help with this ASAP!!!!!!!! For this assignment you will add a copy const
ID: 3878135 • Letter: I
Question
I need help with this ASAP!!!!!!!!
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.
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];
}
// Destructor
~MyVector() {
delete[]list;
}
void push_back(int i);
void pop_back();
int& at(int i);
void clear();
int size();
};
--------------------------------------------------
Test your class using the attached test driver.
Explanation / Answer
Given below is the code for the question. Use the MyVector.h along with the test program given by the instructor.
Please do rate the answer if it was helpful. Thank you
MyVector.h
===========
#include <iostream>
using namespace std;
class MyVector {
private:
int* list;
int max_size;
int list_size;
public:
// Constructor
MyVector() ;
//copy constructor
MyVector(const MyVector &v2);
MyVector& operator = (const MyVector &v2);
bool operator < (const MyVector &v2);
bool operator == (const MyVector &v2);
MyVector operator +(const MyVector &v2);
friend ostream& operator << (ostream& out, const MyVector &v2);
int operator[](int index) const;
int& operator[](int index);
MyVector operator++(int ); //postfix increment
// Destructor
~MyVector();
void push_back(int i);
void pop_back();
int& at(int i);
void clear();
int size();
};
MyVector.cpp
=============
#include "MyVector.h"
// Constructor
MyVector::MyVector() {
max_size = 10;
list_size = 0;
list = new int[max_size];
}
//copy constructor
MyVector::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& 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 MyVector::operator < (const MyVector &v2)
{
return list_size < v2.list_size;
}
bool MyVector::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 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;
}
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 MyVector::operator[](int index) const
{
return list[index];
}
int& MyVector::operator[](int index)
{
return list[index];
}
MyVector 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::~MyVector() {
delete[]list;
}
void MyVector::push_back(int i)
{
if(list_size < max_size)
{
list[list_size++] = i;
}
}
void MyVector::pop_back()
{
if(list_size > 0)
{
list_size--;
}
}
int& MyVector::at(int i)
{
return list[i];
}
void MyVector::clear()
{
list_size = 0;
}
int MyVector::size()
{
return list_size;
}
output
=====
5
[8, 50, 74, 59, 31]
[8, 50, 74, 59, 31]
[9, 51, 75, 60, 32]
[17, 101, 149, 119, 63]
0
0