Question: In C + + there is no check on array index out - of - bounds at compile time and it can cause serious error

In C++ there is no check on array index out-of-bounds at compile time and it can
cause serious error when program runs. And in C++, array index starts at 0.
Your job here is to implement a class MyArray is to solve the out-of-bounds
problem and to start the array index at any integer positive integers. Every
object of type MyArray is an array of type int. During execution, when accessing
an array component, if the index is out-of-bounds, the program must terminate
with an appropriate message.
Consider the following
MyArray list(5);
MyArray myList(2,13);
You may use this for
The first line declares an array of int called list with five elements specified by
list[0], list[1], list[2], list[3] and list[4]
The second line declares an array of int called list with eleven elements specified
by list[2], list[3], list[4].. up to list[12]
Use this for the header file for the class MyArray
#include
#ifndef MyArray_H
#define MyArray_H
using namspace std;
class MyArray
{
public:
const MyArray& operator=(const MyArray& right);
// overload assignment operator
MyArray(int uB);
MyArray(int 1B, int uB);
MyArray();
MyArray(const myArray );
// 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;
int lowerBound;
int upperBound;
};
Then implement all the constructors shown, and all the methods and overloaded
operator shown above.
Test program with this
#include
#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 <<"
Enter 5 integers: ";
for (i =0; i <5; i++)
cin >> list1[i];
cout <<"
After input to list1: ";
for (i =0; i <5; i++)
cout << list1[i]<<"";
list2= list1;
cout <<"
list2 :";
for (i =0; i <5; i++)
cout << list2[i]<<"";
cout <<"
Enter 3 elements: ";
for (i =0; i <3; i++)
cin >> list1[i];
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]<<"";
cou << endl;
list3[2]=7;
list3[4]=8;
list3[3]=54;
list3[5]= list3[4]+ list3[3];
cout << "list3: ";
for (i =2; i <6; i++)
cout << list3[i]<<"";
cout << endl;
return 0;
}
Lastly, redesign the MyArray using templates so it can be used in any application that needs an array.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!