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

-RE-POSTED- This is a C++ advance assigment: Please write out the code and test

ID: 3687199 • Letter: #

Question

-RE-POSTED-

This is a C++ advance assigment: Please write out the code and test it works 100% in Visual Studio. Use a format so I can copy and paste the code into my visual studio. If instructions are needed on how to properly copy and paste to make the code work then please advise. No NOT answer unless the program works 100% as instructed in assigment. This assigment is due april 11th. Thank you.

HW 13 – Part1

Visit the following link for an example on how to override the array subscript operator:

http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/

Assignment:

Recall that the only built-in operations on classes are the assignment operator and the member selection operator. Therefore, other operators cannot be directly applied to class objects by default. However, C++ allows the programmer to extend the definitions of operators such as relational operators, arithmetic operators, the insertion operator for data output, and the extraction operator for data input—so they can be applied to classes.

Use attached myarray.h and test file,

1. implement the class myArray that overrides the following operators: =, [], ==, !=, <<, >>

2. Implement the copy constructor

3. Run the test program to verify your output matches the output screen below.

Note:

1. Make sure you do a deep copy when you implement operator = and copy constructor

2. When you overload operator [], check if the index is in the boundary. If not, use assert to abort the program.

MainProgram.cpp

#include <iostream>
#include "myArray.h"

using namespace std;

int main()
{
myArray list1(5);
myArray list2(5);

int i;

cout << "list1 : " << list1 << endl;

cout << "Enter 5 integers: ";
cin >> list1;
cout << endl;

cout << "After filling list1: " << list1 << endl;

list2 = list1;
cout << "list2 : " << list2 << endl;

   if (list2 == list1)
   cout << "list1 == list2 " << endl;

list1[0] = list1[1] + list1[2];
cout << "After list1[0] = list1[1] + list1[2]: " << list1 << endl;

   if (list2 != list1)
       cout << "list1 != list2 " << endl;

system("pause");
return 0;
}

myArray.h

#ifndef H_myArray
#define H_myArray

#include <iostream>

using namespace std;

class myArray
{
friend ostream& operator<< (ostream&, const myArray&);
friend istream& operator>> (istream&, myArray&);

public:
const myArray& operator= (const myArray &right);
//overload assignment operator

myArray(int uB);

myArray(const myArray &copy);
//copy constructor

~myArray();
//destructor

int &operator[] (int);
const int &operator[](int) const;

//overload relational operator
bool operator== (const myArray &right) const;
bool operator!= (const myArray &right) const;

private:
int *aPtr; //pointer to int array
int size;
};

#endi

Explanation / Answer

//myArray.cpp

//myArray.cpp
#include <stdlib.h>
#include <iostream>
#include "myArray.h"
using namespace std;
int myArray::getSize()
{
   return size;
}
int* myArray::getPtr()
{
   return aPtr;
}
ostream& operator<<(ostream& out, const myArray& A)
{
   int *p=A.getPtr();
   for(int i=0;i<A.getSize();i++)
   {
       out<<*(p+i)<<" ";
   }
   return out;
}
istream& operator>> (istream& in, const myArray& A)
{
   int *p=A.getPtr();
   for(int i=0;i<5;i++)
   {
       in>>(p+i);
   }
   return in;
}

//overload assignment operator
const myArray& myArray::operator= (const myArray &right)
{
   myArray left(right.getSize());
   int *p=left.getPtr();
   p=right.getPtr();
   return left;
}
//copy constructor
myArray::myArray(const myArray &copy)
{
   size=copy.getSize();
   aPtr=copy.getPtr();
}

//destructor
myArray::~myArray()
{
   delete aPtr;
}
myArray::myArray(int uB)
{
   size=uB;
   aPtr= (int *)malloc(sizeof(int)*uB);
}
int& myArray:: operator[] (int h)
{
   return *(aPtr+h);
}

//overload relational operator
bool myArray::operator== (const myArray &right) const
{
   bool f=true;
   if(size==right.getSize())
   {
       int *p=right.getPtr();
       for(int i=0;i<size;i++)
       {
           if(*(aPtr+i)==*(p+i))
           {
               f=true;
           }
           else
           {
               f=false;
               break;
           }
       }
   }
   else
   {
       f=false;
               break;
}
   return f;
}

bool operator!= (const myArray &right) const
{
   bool f=true;
   if(size!=right.getSize())
   {
       f=false;
       break;
   }
   else
   {
       int *p=right.getPtr();
       for(int i=0;i<size;i++)
       {
           if(*(aPtr+i)!=*(p+i))
           {
               f=false;
               break;
           }
       }
   }
   return f;
}

//myArray.h

//myArray.h
#ifndef H_myArray
#define H_myArray
#include <iostream>

using namespace std;
class myArray
{
friend ostream& operator<< (ostream&, const myArray&);
friend istream& operator>> (istream&, myArray&);
public:
const myArray& operator= (const myArray &right);
//overload assignment operator
myArray(int uB);
myArray(const myArray &copy);
//copy constructor
~myArray();
//destructor
int &operator[] (int);
//const int &operator[](int) const;
//overload relational operator
bool operator== (const myArray &right) const;
bool operator!= (const myArray &right) const;
int getSize();
int* getPtr();
private:
int *aPtr; //pointer to int array
int size;
};
#endif