Question: 1 Introduction The C++ array is too primitive and is easy to induce errors. In this project, we will develop a class to overcome some

1 Introduction

The C++ array is too primitive and is easy to induce errors. In this project, we will develop a class to overcome some of the problems for integer arrays. We call this class xArray, which has three private member: data is the int array, len (of type size t 1 ) to remember the actual length of the array, and arraySize to remember how big the array we allocate. Your code should be able to handle arbitrary large arrays, and has no memory violation or memory leak. Since you cannot predict how many integers the array has, you must find a way to dynamically grow the integer array.

2 Files to turn in:

You need to turn in four files: Makefile, main.C, xArray.C, xArray.h. Among them, you need to implement main.C and xArray.C, I will provide xArray.h and Makefile which you cannot modify at all. You need to write a main.C file to test the functions and operators. Although I will use my own main.c to test your code, you still need to turn in yours. xArray.h declares the class xArray. In this file, I have implemented several member functions, but you need to implement all other member functions and overloaded operators in xArray.C.

3 Functions:

All member function and operator prototypes are declared in xArray.h, with necessary comments before each function. The following are major functions: PushBack function is the core of this project, and should be used by other functions if necessary (for example, it can be used by the += and >> operators). This function put the integer at the end of the array. Since the array may not have enough space to hold it, you need to grow the array if necessary. [ ] operator should check the boundary of data, if idx is not within the boundary, print error message and exit the program; otherwise, return data[idx]; >> operator should read in an integer and put it at the end of the array.

4 Other Requirements:

The total points is 10. Three extra points will be given to people who implement the PushFront function efficiently. This function will put an integer at the beginning of an array. If you do not want the extra points, you do not need to implement this function at all. No late turn in is acceptable, any late turn in will be given 0 points.

Things you need: xArray.h file

#ifndef XARRAY_H_ #define XARRAY_H_ #include  #include  #include  using namespace std; class xArray{ size_t len; size_t arraySize; //data of int array int* data; public: // Constructors xArray(); xArray(const xArray& a); xArray(const int x);//create an array with x elements //copy constructor to get n integer from an C style array, //n is assumed to be < the length of of that array xArray(const int * a, size_t n); // Destructor ~xArray(); // Assignment and modification operators xArray& operator=(const xArray& right); //make the array hold only one integer xArray& operator=(const int c); //merge two arrays, append all elements from right to the end of this xArray& operator+=(const xArray& right); //add c to the end of this array xArray& operator+=(const int c); // Put an int at the end of data, can be used by operators "+=", ">>" size_t PushBack(const int c); // set len to 0, i.e. the array is cleared void Clear(void); // index operator, if idx over bound, print message and quit int operator[](size_t idx) const; //get the length of the xArray& size_t Length() const { return len; } //overloaded << operator, print all elements of the array. A line can at most //hold 20 elements friend ostream& operator<<(ostream& out, const xArray& a); //>> operator, get one int and put it at the end of the array friend istream& operator>>(istream& in, xArray& a); }; #endif 

And the makefile:

CC= g++ CCFLAGS= -g -Wall exec: main.o xArray.o makefile $(CC) $(CCFLAGS) -o intTest main.o xArray.o main.o: main.C xArray.h $(CC) $(CCFLAGS) -c main.C xArray.o: xArray.C xArray.h $(CC) $(CCFLAGS) -c xArray.C clean: rm -rf *.o 

Example main.C:

#include "xArray.h" int main() { int* tmp = new int[40]; for (int i = 0; i < 40; i++) tmp[i] = i; xArray a; /* uncomment the following lines one by one once you have implemented the * functionality*/ //xArray d(tmp, 40); //cout << "First d: " << d << endl; //xArray b(10); //xArray c(d); //cout << "First c: " << c << endl; //a = 0; //for (int i = 0; i < 10; i++) a += i; //cout << "First a: " << a << endl; //b = a; //cout << "First b: " << b << endl; //d = b; //cout << "Second d:" << d << endl; //b+=a; //cout << "Second b:" << b << endl; //b+=1000; //cout << "Third b: " << b << endl; //cout << "The fifth element of b is " << b[4] << endl; //b.Clear(); //cout << "Please input an int: "; //cin >> b; //cout << "Forth b: " << b << endl; delete [] tmp; return 0; } 

Example xArray.C (work from this to compile):

#include  #include  #include  #include  #include "xArray.h" using namespace std; // Constructors xArray::xArray() { len = 0; data = new int[10]; arraySize = 10; } // Destructor xArray::~xArray() { delete [] data; } 

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!