In: Computer Science
Modify the object provided in the code below to include a variety of overloaded operators in C++. Your object should have the following member variables:
1. float *arr
a. A dynamic float array that constitutes the data in your myArray object.
2. int size
a. The size of the array that the myArray object holds
Modify the provided code to include a variety of overloaded operators
Operators to overload:
1. bool operator!=(myArray& obj2)
a. Tests to see if the calling object is identical (in size and elements) to the myArray object passed in (obj2).
2. float operator[](int index)
a. Takes in an integer (index) and returns the element at that index in arr.
3. void operator()(int index, float num)
a. Changes the value of arr at index to num.
4. myArray& operator=(myArray obj2)
a. Assignment operator. Should set the calling object to the values of the object passed in (obj2).
b. Should be cascade capable.
5. myArray operator+(myArray& obj2)
a. Should compute the pairwise addition of values between the calling object and the object passed in (obj2). You may assume that the two objects have the same size.
b. Ex. [1,1,1] + [2,2,2] = [3,3,3]
c. Should be cascade capable.
6. void operator+=(myArray&)
a. Should change the calling object’s data to a concatenation of the calling object’s previous values and the values of the object passed in.
b. Ex. [1,2] + [3,4,5] = [1,2,3,4,5]
7. friend istream& operator>>(istream& in, myArray& rhs)
a. Should ask the user to enter n number of floats into the arr variable (where n is the size of the object)
b. Should be cascade capable.
8. friend ostream& operator<<(ostream& out, myArray& rhs)
a. Should print the array to standard out. If size == 0 then it should print NULL.
b. Should be cascade capable.
Additional Specifications:
● Your program should have NO memory leaks
● You should not use the built-in C++ vector class anywhere in your object.
● You must appropriately use the const keyword (you need to decide which functions will be constant qualified and which will not).
● All functions should be named as stated above.
*** myArray.cpp ***
#include "myArray.h"
myArray::myArray()
{
size = 0;
arr = new float[size];
}
myArray::myArray(int _size, float _num)
{
size = _size;
arr = new float[_size];
for(int i = 0; i < size; i++)
{
arr[i] = _num;
}
}
myArray::myArray(float* _arr, int _size)
{
size = _size;
arr = new float[_size];
for(int i = 0; i < size; i++)
{
arr[i] = _arr[i];
}
}
myArray::myArray(const myArray& obj2)
{
size = obj2.size;
arr = new float[size];
for(int i = 0; i < size; i++)
{
arr[i] = obj2.arr[i];
}
}
myArray::~myArray()
{
delete [] arr;
}
void myArray::insert(int _index,float _num)
{
float *temp;
temp = arr;
arr= new float[size+1];
for(int i = 0; i < _index; i++)
{
arr[i] = temp[i];
}
arr[_index] = _num;
for(int i = _index+1; i < size+1; i++)
{
arr[i] = temp[i-1];
}
size = size +1;
delete [] temp;
}
void myArray::remove(int _index)
{
float *temp;
temp = arr;
arr = new float[size-1];
for(int i = 0; i < size-1; i++)
{
if(i != _index && i <
_index)
{
arr[i] =
temp[i];
}
else
{
arr[i] =
temp[i+1];
}
}
delete [] temp;
size = size -1;
}
float myArray::get(int _index)
{
if(_index >=0 && _index < size)
{
return arr[_index];
}
cout << "Error, index out of bounds" <<
endl;
return -1;
}
void myArray::clear()
{
delete [] arr;
arr = new float[0];
size = 0;
}
int myArray::find(float _num)
{
int ret = -1;
for(int i = 0; i < size; i++)
{
if(arr[i] == _num)
{
ret = i;
break;
}
}
return ret;
}
bool myArray::equals(myArray& obj2)
{
bool ret = true;
for(int i = 0; i < size; i++)
{
if(arr[i] != obj2.arr[i])
{
ret =
false;
break;
}
}
return ret;
}
void myArray::init()
{
cout << "Enter " << size << "
elements." << endl;
for(int i = 0; i < size; i++)
{
cin >> arr[i];
}
}
void myArray::print()
{
for(int i = 0; i < size; i++)
{
cout << arr[i] << "
";
}
cout << endl;
}
*** myArray.h ***
#include <iostream>
using namespace std;
class myArray {
friend istream& operator>>(istream&,
myArray&);
friend ostream& operator<<(ostream&,
myArray&);
public:
myArray();
myArray(int,float);
myArray(float*, int);
myArray(const myArray&);
~myArray();
void insert(int,float);
void remove(int);
float get(int);
void clear();
int find(float);
bool equals(myArray&);
void init();
void print();
float operator[](int);
void operator()(int, float);
myArray&
operator=(myArray);
//a1 = a2
myArray
operator+(myArray&);
void
operator+=(myArray&);
//a1 += a2
private:
float* arr;
//T* arr;
int size;
};
// MYARRAY.H
#include <iostream>
using namespace std;
class myArray {
friend istream& operator>>(istream&, myArray&);
friend ostream& operator<<(ostream&, myArray&);
public:
myArray();
myArray(int,float);
myArray(float*, int);
myArray(const myArray&);
~myArray();
void insert(int,float);
void remove(int);
float get(int);
void clear();
int find(float);
bool equals(myArray&);
void init();
void print();
float operator[](int);
void operator()(int, float);
void operator=(myArray);
//a1 = a2
myArray operator+(myArray&);
void operator+=(myArray&);
//a1 += a2
bool operator!=(const myArray &)const;
private:
float* arr;
//T* arr;
int size;
};
#include "myArray.h"
myArray::myArray()
{
size = 0;
arr = new float[size];
}
myArray::myArray(int _size, float _num)
{
size = _size;
arr = new float[_size];
for(int i = 0; i < size; i++)
{
arr[i] = _num;
}
}
myArray::myArray(float* _arr, int _size)
{
size = _size;
arr = new float[_size];
for(int i = 0; i < size; i++)
{
arr[i] = _arr[i];
}
}
myArray::myArray(const myArray& obj2)
{
size = obj2.size;
arr = new float[size];
for(int i = 0; i < size; i++)
{
arr[i] = obj2.arr[i];
}
}
myArray::~myArray()
{
delete [] arr;
}
void myArray::insert(int _index,float _num)
{
float *temp;
temp = arr;
arr= new float[size+1];
for(int i = 0; i < _index; i++)
{
arr[i] = temp[i];
}
arr[_index] = _num;
for(int i = _index+1; i < size+1; i++)
{
arr[i] = temp[i-1];
}
size = size +1;
delete [] temp;
}
void myArray::remove(int _index)
{
float *temp;
temp = arr;
arr = new float[size-1];
for(int i = 0; i < size-1; i++)
{
if(i != _index && i < _index)
{
arr[i] = temp[i];
}
else
{
arr[i] = temp[i+1];
}
}
delete [] temp;
size = size -1;
}
float myArray::get(int _index)
{
if(_index >=0 && _index < size)
{
return arr[_index];
}
cout << "Error, index out of bounds" << endl;
return -1;
}
void myArray::clear()
{
delete [] arr;
arr = new float[0];
size = 0;
}
int myArray::find(float _num)
{
int ret = -1;
for(int i = 0; i < size; i++)
{
if(arr[i] == _num)
{
ret = i;
break;
}
}
return ret;
}
bool myArray::equals(myArray& obj2)
{
bool ret = true;
for(int i = 0; i < size; i++)
{
if(arr[i] != obj2.arr[i])
{
ret = false;
break;
}
}
return ret;
}
void myArray::init()
{
cout << "Enter " << size << " elements." << endl;
for(int i = 0; i < size; i++)
{
cin >> arr[i];
}
}
void myArray::print()
{
for(int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
float myArray::operator[](int ind){
if(ind >=0 and ind < size)return arr[ind];
else{
cout<<"Index out of bounds\n";
exit(0);
}
}
void myArray::operator()(int ind, float num){
if(ind >=0 and ind < size) arr[ind] = num;
else{
cout<<"Index out of bounds\n";
exit(0);
}
}
void myArray::operator=(myArray other){
int n = other.size;
delete [] arr;
arr = new float[n];
for(int i=0;i<n;i++){
arr[i] = other.arr[i];
}
size = n;
}
myArray myArray::operator+(myArray& other){
myArray temp;
temp.size = other.size;
for(int i=0;i<size;i++){
temp.arr[i] = arr[i] + other.arr[i];
}
return temp;
}
void myArray::operator+=(myArray& other){
for(int i=0;i<size;i++){
arr[i] = arr[i] + other.arr[i];
}
}
ostream& operator<<(ostream& out, myArray& rhs){
if(rhs.size == 0){
out<<"NULL ";
return out;
}
for(int i=0;i<rhs.size;i++){
out<<rhs.arr[i]<<" ";
}
cout<<endl;
return out;
}
istream& operator>>(istream& in, myArray& rhs){
cout<<"Enter the size : ";
in>>rhs.size;
cout<<"Enter "<<rhs.size<<" values : ";
for(int i=0;i<rhs.size; i++)in>>rhs.arr[i];
return in;
}
int main(){
myArray a;
cin>>a;
cout<<a;
}
SAVE THE CODE IN RESPECTIVE FILENAME AND THEN RUN