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

Declare a specification file for a class and save it as myArray.h, implement tha

ID: 3672191 • Letter: D

Question

Declare a specification file for a class and save it as myArray.h, implement that class and save it as myArrayImp.cpp and test that class. Impement the single parameter constructor, two parameters constructor and the destructor. Overload the subscript operater [] for non-constant arrays, and then return a pointer of the array component or terminate program if the specific index is out of range. Overload the subscript operator [] for constant arrays, and then return a pointer of the array component or terminate program if the specified index is out of range. Create three objects list, myList, and yourList for myArray class. Insert elements into all three arrays, display the integers stored in all three arrays, and then Test the index out of bound problem.

myArray.h   file is blank and so is myArrayImp.cpp   However, my main I have the code below. How would I get all three files to match the instructions listed above?

//main file


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

using namespace std;

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

    int i;

    cout << "list1 : ";
    for (i = 0 ; i < 5; i++)
        cout << list1[i] <<" ";
    cout<< endl;

    cout << "Enter 5 integers: ";
    for (i = 0 ; i < 5; i++)
        cin >> list1[i];
    cout << endl;

    cout << "After filling list1: ";

    for (i = 0 ; i < 5; i++)
        cout << list1[i] <<" ";
    cout<< endl;

    list2 = list1;
    cout << "list2 : ";
    for (i = 0 ; i < 5; i++)
        cout << list2[i] <<" ";
    cout<< endl;

    cout << "Enter 3 elements: ";

    for (i = 0; i < 3; i++)
        cin >> list1[i];
    cout << endl;

    cout << "First three elements of list1: ";
    for (i = 0; i < 3; i++)
        cout << list1[i] << " ";
    cout << endl;

    myArray list3(-2, 6);

    cout << "list3: ";
    for (i = -2 ; i < 6; i++)
        cout << list3[i] <<" ";
    cout<< endl;

    list3[-2] = 7;
    list3[4] = 8;
    list3[0] = 54;
    list3[2] = list3[4] + list3[-2];

    cout << "list3: ";
    for (i = -2 ; i < 6; i++)
        cout << list3[i] <<" ";
    cout<< endl;
   system("pause");

    return 0;
}

Explanation / Answer

If you need any clarification/help please comment ont the answer

myArray.h

#pragma once
class myArray
{
public:
   myArray(int size);
   myArray(int sIndex,int eIndex);
   ~myArray();
   int& operator[](int i);
   const int& operator[](const int i) const;
private:
   int startIndex;
   int endIndex;
   int* value;
};

myArray.cpp

#include "myArray.h"
#include <iostream>
using namespace std;


myArray::myArray(int size)
{
   startIndex = 0;
   endIndex = size - 1;
   value = new int[size];
}

myArray::myArray(int sIndex, int eIndex)
{
   startIndex = sIndex;
   endIndex = eIndex - 1;
   value = new int[eIndex - sIndex];
}

myArray::~myArray()
{
   delete value;
}

int &myArray::operator[](int i)
{
   if (i > endIndex)
   {
       cout << "Index out of bounds" << endl;
       exit(-1);
   }
   return value[i];
}

const int &myArray::operator[](const int i)const
{
   if (i > endIndex)
   {
       cout << "Index out of bounds" << endl;
       exit(-1);

   }
   return value[i];
}

test.cpp(added one case for out of bounds value,program terminates after that)

#include <iostream>
#include "myArray.h"
using namespace std;

int main()
{
   myArray list1(5);
   myArray list2(5);
   int i;
   cout << "list1 : ";
   for (i = 0; i < 5; i++)
       cout << list1[i] << " ";
   cout << endl;
   cout << "Enter 5 integers: ";
   for (i = 0; i < 5; i++)
       cin >> list1[i];
   cout << endl;
   cout << "After filling list1: ";
   for (i = 0; i < 5; i++)
       cout << list1[i] << " ";
   cout << endl;
   list2 = list1;
   cout << "list2 : ";
   for (i = 0; i < 5; i++)
       cout << list2[i] << " ";
   cout << endl;
   cout << "Enter 3 elements: ";
   for (i = 0; i < 3; i++)
       cin >> list1[i];
   cout << endl;
   cout << "First three elements of list1: ";
   for (i = 0; i < 3; i++)
       cout << list1[i] << " ";
   cout << endl;
   myArray list3(-2, 6);
   cout << "list3: ";
   for (i = -2; i < 6; i++)
       cout << list3[i] << " ";
   cout << endl;
   list3[-2] = 7;
   list3[4] = 8;
   list3[0] = 54;
   list3[2] = list3[4] + list3[-2];
   cout << "list3: ";
   for (i = -2; i < 6; i++)
       cout << list3[i] << " ";
   cout << endl;
   //checking for an out of bounds value
   //program should terminate after this
   cout << list3[8];
   system("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote