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

I need help with this ASAP!!!!!!!! For this assignment you will add a copy const

ID: 3878123 • 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.  

-------------------------------

MyVector.h file:

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();

};

------------

MyVector.cpp file:

#include

#include

#include "foo.h"

using namespace std;

void MyVector::push_back(int i) {

if (list_size == max_size)

cout << "ERROR: Cannot insert " << i << ". List is full";

else {

list[list_size] = i;

list_size++;

}

}

void MyVector::pop_back() {

if (list_size == 0)

cout << "Cannot pop_back as list is already empty";

else {

list_size--;

}

}

int& MyVector::at(int i) {

return list[i];

}

void MyVector::clear() {

list_size = 0;

}

int MyVector::size() {

return list_size;

}

-----------------------------------

Test your class using the attached test driver.

#include "MyVector.h"

#include

using namespace std;

#include

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;

}

Explanation / Answer

//MyVector.h

#ifndef MYVECTOR_H
#define 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];
}
// Destructor
~MyVector() {
// delete[] list;
}
  
void push_back(int i);
void pop_back();
int& at(int i);
void clear();
int size();
bool operator< (MyVector v);
bool operator== (MyVector v);
void operator= (MyVector v);
MyVector operator+ (MyVector v);
friend ostream& operator<< (ostream &dout , MyVector &v);
void operator++(int);
int& operator[](unsigned int i);
};
#endif

//MyVector.cpp

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include "MyVector.h"
using namespace std;

void MyVector::push_back(int i) {
if (list_size == max_size){
cout << "ERROR: Cannot insert " << i << ". List is full";
}   
else {
list[list_size] = i;
list_size++;
}
}
void MyVector::pop_back() {
if (list_size == 0){
cout << "Cannot pop_back as list is already empty";
}
else {
list_size--;
}
}


int& MyVector::at(int i) {
return list[i];
}


void MyVector::clear() {
list_size = 0;
}
int MyVector::size() {
return list_size;
}

bool MyVector::operator< (MyVector v){
bool result;
result = (list_size < v.list_size) ? true : false;
return result;
}


bool MyVector::operator== (MyVector v){
bool result;
if(list_size == v.list_size){
for(int i=0 ;i < list_size; i++){
if( at(i) == v.at(i) ) {
result = true;
continue;
}
else{
result = false;
break;
}
}
}
else{
result = false;
}
return result;
}

void MyVector::operator= (MyVector v){
clear();
list_size = v.size();
for(int i=0 ;i < v.list_size; i++){
at(i) = v.at(i);
}  
}


MyVector MyVector::operator+ (MyVector v){
MyVector temp;
temp.list_size = (size() > v.size()) ? size() : v.size();
int fisrt_smaller_size_flag = (size() < v.size()) ? 1 : 0;
  
if(fisrt_smaller_size_flag){
for(int i=0 ;i < size(); i++){
temp.at(i) = at(i) + v.at(i) ;
}
for(int i= size() ; i < v.size() ; i++){
temp.at(i) = v.at(i) ;
}
}
else{
for(int i=0 ;i < v.size(); i++){
temp.at(i) = at(i) + v.at(i) ;
}
for(int i= v.size() ; i < size() ; i++){
temp.at(i) = at(i) ;
}  
}
return temp;  
}

ostream& operator<< (ostream &dout , MyVector &v){  
dout << "[" << v.at(0);
for(int i=1 ;i < v.list_size; i++){
dout << ", " << v.at(i);
}  
dout << "]";
return dout;
}


void MyVector::operator++(int){
for(int i=0 ;i < list_size; i++){
int val = at(i);
val++;
at(i) = val;
}  
}

int& MyVector::operator[](unsigned int i){
return list[i];
}

//test.cpp

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include "MyVector.h"
using namespace std;


int main()
{
MyVector m;
for(int i = 0; i < 5; i++)
m.push_back(1 + rand() % 100);
  
MyVector test ;
for(int i = 0; i < 6; i++)
test.push_back(1 + rand() % 100);

cout <<( m < test) << " ok1" <<endl;
cout <<( test < m) << " ok2" <<endl;
cout << (test == m) << " ok3" <<endl;
cout << (test == test) << " ok4" <<endl;
cout << test << endl;
cout << m << endl;
MyVector add ;
cout << add;
add = test + m ;
cout << add;
add++;
cout << add;
cout << (add[3]) <<endl;
add[3] = 999;
cout << add;

// 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;
}