Question
C++ Programming (7th Edition) Recall that in C++, there is no check on an array index out of bounds. However, during program execution, an array
C++ Programming (7th Edition)
Recall that in C++, there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++, the array index starts at 0. Design and implement the class myArray that solves the array index out of bounds problem and also allows the user to begin the array index starting at any integer, positive or negative. 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 error message. Consider the following statements: myArray list(5); //Line 1 myArray myList(2, 13); //Line 2 myArray yourList(-5, 9); //Line 3 The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], ..., list[4]; the statement in Line 2 declares myList to be an array of 11 components, the component type is int, and the components are: myList[2], myList[3], ..., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], ..., yourList[0], ..., yourList[8]. Write a program to test the class myArray.
i am getting an error ;Desktop\maina.cpp [Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]
\Desktop\maina.cpp [Note] (if you use '-fpermissive' G++ will accept your code)
Here is the program
//main.cpp
#include
#include "myArray.h"
using namespace std;
int main()
{
cout<<"n \t A program to check the functionality "
<<" \tof the class myArray.h. ";
myArray list(5);
myArray myList(2,13);
myArray yourList(-5,9);
for (int i=0; i<5; i++)
list[i]=(i*3 +65);
for (int i=2; i<13; i++);
myList[i]=i * 3;
for(int i=-5; i<9; i++)
yourList[i]= i*2;
cout<<" The elments stored in the first object list are:"< for (int i=0; i<5;i++) { cout<<"list["< } cout<<" The elments stored in the second object myList are:"< for (int i=2; i<13;i++) { cout<<"myList["< } cout<<" The elments stored in the third object yourList are:"< for (int i=-5; i<9;i++) { cout<<"yourList["< } cin>>yourlist[-13] system("pause"); return 0; } //myArray.h #ifndef H_myArray #define H_myArray #include using namespace std; class myArray{ public: myArray(int); myArray(int, int); ~myArray(); int &operator[](int); int operator [](int)const; private: int size; int startIndex; int *PtrArray; }; #endif //myArray.cpp #include #include "myArray.h" using namespace std; myArray::myArray(int newSize) { startIndex =0; size =newSize; PtrArray=new int[size]; for(int i=0; i PtrArray[i]=0; } myArray::myArray(int newStartIndex, int newSize) { startIndex =-newStartIndex; size=newSize; PtrArray=new int[size+ startIndex]; for(int i=0; i<(size + startIndex);i++) PtrArray[i]=0; } myArray::~myArray() { delete[]PtrArray; } int &myArray::operator[](int index) { if(index <-startIndex|| index >= size) { cerr<<" Error:Index" < <<"is out of range"< exit(1); } return PtrArray[index + startIndex]; } int myArray::operator[](int index)const { if(index <-startIndex||index>=size) { cerr<<" Error:Index"< <<"is out of range"< exit(1); } return PtrArray[index + startIndex]; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started