EGRE 246 Advanced Engineering Programming Using C++ Homework #8 – Operator overloading

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (5 votes)

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 array{
public:
array();
array(int);
array(const array&);
~array();
array& operator=(const array&);
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];}

void print(void);
private:
double *data;
int elements;
};
You will have to implement three constructors:
1. Default constructor that does not allocate any memory and will set the elements
member to zero.
2. 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.
3. 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.
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)’.
2. 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.
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] = a[1] + b, …
The subscript[] 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.
For this homework, 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.