Help, Implement an array class operator overload! For this homework you will hav
ID: 3762438 • Letter: H
Question
Help, Implement an array class operator overload!
For this homework you will have to implement an array class which supports the addition overloaded operator, the assignment operator and the array subscript operator. The elements of the array will be of the double type class arrayt public: array) array (int); array (const array&); array)i array& operator- (const array&) friend array operatort (double, const array&) friend array operatort (const array &, const array&); friend array operatort (const array &, double); double& operator [] (int index) (return this->data [index]) void print (void); private: double *data; int elements; You will have to implement three constructors Default constructor that does not allocate any memory and will set the elements member to zero Custom constructor that will allocate the number of elements (using the 'new operator) indicated by the argument and set the private elements member accordingly. Each element in the array (data) should be set to zero Copy constructor that will allocate the right amount of elements determined by the object to copy from, set the elements member correctly, and copy over all the data. 1. 2. 3. The desctructor will deallocate all the memory for that member using the 'delete' operator You will also have to implement the following overloaded operators: 1. Assignment operator, make sure that the size of each array (number of elements) is equal. If the size is different you will have to send the user a message and exit the program using 'exit( 1) Overloaded addition operators, make sure where applicable you check the size of both arrays. If the size of both arrays do not match than warn the user and exit the program 2. a. Addition of two arrays: z[0] = a[0] + b[0], z[1] = a[1] + b[1] b. Addition of array and constant z[0]a[0] + b, z[1+ b, The subscriptl operator is already implemented in the header file where it returns a reference to an element in the array. This subscript operator gives you access to each individual element of the array indicated by the index in []. You can use this subscript operator to read from the array or write to the array This homework will provide you a main file that will test the different functionalities of the array class To succeed with this homework you will have to 1. Implement all the methods of the array class (constructors, destructor, and overloaded operators) 2. Make the code compile and run with the main file given to you. submit a zip file with the following files 1. Makefile 2. main.cpp 3. array.cpp 4. array.h Below is a screenshot of the program output. prog 13 20 13 arrays are not equal sizeExplanation / Answer
class array {
public:
array() {
elements=0;
}
array(int n) {
elements=n;
data = new double[n];
for (int i=0; i<elements; i++) {
data[i]=0;
}
}
array(const array &n) {
elements = n.elements;
data = new double[elements];
for (int i=0; i<elements; i++) {
data[i]=n.data[i];
}
}
~array() {
delete[] data;
}
array& operator=(const array &n) {
array temp;
temp.elements = n.elements;
temp.data = new double[temp.elements];
for (int i=0; i<temp.elements; i++) {
temp.data[i]=n.data[i];
}
return temp;
}
friend array operator+(double,const array&);
friend array operator+(const array&, const array&);
friend array operator+(const array&, double);
double& operator[](int index){
return this->data[index];
}
private:
double *data;
int elements;
};
array operator+(double b,const array &n) {
array temp(n);
for (int i=0; i<temp.elements; i++) {
temp.data[i]+=b;
}
return temp;
}
array operator+(const array &n1, const array &n2) {
if (n1.elements != n2.elements) {
exit(1);
}
array temp(n1);
for (int i=0; i<n2.elements; i++) {
temp.data[i]+=n2.data[i];
}
return temp;
}
array operator+(const array &n, double b) {
array temp(n);
for (int i=0; i<temp.elements; i++) {
temp.data[i]+=b;
}
return temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.