In: Computer Science
Description:
Modify your myArray object to include a variety of overloaded operators. You may start with your implementation or start from the code attached (posted after Project 3 due date).
Your object should have the following member variables (same as before):
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
Your object should have the following constructors and functions (all from the previous assignment):
1. myArray()
2. myArray(int _size, float num) 3. myArray(float *_arr, int _size)
4. ~myArray()
5. insert(int index, float num)
6. remove(int index)
7. get(int index)
8. clear()
9. find(float num)
10. equals(myArray & obj2)
11. init()
12. print()
New 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.
A header will be provided in which all function prototypes will be given. You may add to this file, but do not change the portion that is provided!
You will need to submit two files: myArray.h and myArray.cpp. Do not submit the executable!
Main.cpp
#include <cstdlib>
#include <iostream>
#include "myArray.h"
using namespace std;
int main(int argc, char** argv)
{
cout << "This main will test operator[] and operator()" << endl;
float *temp;
temp = new float[3];
for(int i = 0; i < 3; i++) temp[i] = i;
myArray a1(temp, 3);
delete [] temp;
cout << "----------------------------------------------------" << endl;
cout << "Testing operator[] with legal index (should print out 1): " << endl;
cout << a1[1] << endl;
cout << "----------------------------------------------------" << endl;
cout << "Testing operator[] with illegal index (should print out Error: index not found twice): " << endl;
cout << a1[-1] << endl;
cout << a1[3] << endl;
cout << "----------------------------------------------------" << endl;
cout << "Testing operator() with legal index (should print out 1.1 1 2): " << endl;
a1(0,1.1);
a1.print();
cout << "----------------------------------------------------" << endl;
cout << "Testing operator() with illegal index (should print out Error: index not found twice and then 1.1 1 2 - no change should be made): " << endl;
a1(-1,1.1);
a1(3,1.1);
a1.print();
return 0;
}
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();
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.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()
{
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;
}
Code:
myArray.h
//myArray.h
#include <iostream>
using namespace std;
class myArray {
//stream overloading
friend istream& operator>>(istream&, myArray&);
friend ostream& operator<<(ostream&, myArray&);
public:
myArray();
myArray(int,float);
myArray(float*, int);
~myArray();
void insert(int,float);
void remove(int);
float get(int);
void clear();
int find(float);
bool equals(myArray&);
void init();
void print();
//new opeartors
bool operator!=(myArray&);
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.cpp
//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()
{
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;
}
//new opeartors
bool myArray::operator!=(myArray& other){
bool flag = (this->size == other.size );
return (flag && this->equals(other));
}
float myArray::operator[](int index){
return get(index);
}
void myArray::operator()(int index, float num){
if(index<0 || index>=size){
cout<<"Invalid index:" << endl;
return;
}
arr[index] = num;
}
myArray& myArray::operator=(myArray other){
if(this->size != other.size){
cout<<"Objects are of different size"<<endl;
return (*this);
}
for(int i=0; i<this->size; i++){
(this->arr)[i] = other[i];
}
return *this;
}
myArray myArray::operator+(myArray& other){
int s = this->size;
myArray temp(s, 0);
for(int i=0; i<s; i++){
temp.arr[i] = (this->arr)[i] + other[i];
}
return temp;
}
void myArray::operator+=(myArray& other){
int s = this->size, total_size;
total_size = s + other.size;
float * temp = this->arr;
arr = new float[total_size];
for(int i=0; i<s; i++){
arr[i] = temp[i];
}
for(int i=s; i<(total_size); i++){
arr[i] = other[i-s];
}
this->size = total_size;
delete [] temp;
}
istream& operator>>(istream& is, myArray& other){
int s = other.size;
cout << "Enter "<<s<<" float values:"<<endl;
for(int i=0; i<s; i++){
is >> other.arr[i];
}
return is;
}
ostream& operator<<(ostream& os, myArray& other){
if(other.size == 0){
os << "NULL" ;
}
else{
for(int i=0; i<other.size; i++){
os << other[i] << " ";
}
}
return os;
}
main.cpp
#include <cstdlib>
#include <iostream>
#include "myArray.h"
using namespace std;
int main(int argc, char** argv)
{
cout << "This main will test operator[] and operator()" << endl;
float *temp;
temp = new float[3];
for(int i = 0; i < 3; i++) temp[i] = i;
myArray a1(temp, 3);
delete [] temp;
cout << "----------------------------------------------------" << endl;
cout << "Testing operator[] with legal index (should print out 1): " << endl;
cout << a1[1] << endl;
cout << "----------------------------------------------------" << endl;
cout << "Testing operator[] with illegal index (should print out Error: index not found twice): " << endl;
cout << a1[-1] << endl;
cout << a1[3] << endl;
cout << "----------------------------------------------------" << endl;
cout << "Testing operator() with legal index (should print out 1.1 1 2): " << endl;
a1(0,1.1);
//a1.print();
cout<<a1 <<endl;
cout << "----------------------------------------------------" << endl;
cout << "Testing operator() with illegal index (should print out Error: index not found twice and then 1.1 1 2 - no change should be made): " << endl;
a1(-1,1.1);
a1(3,1.1);
//a1.print();
cout<<a1 <<endl; //print through overloaded stream
return 0;
}
Output: