Question

In: Computer Science

Description: Modify your myArray object to include a variety of overloaded operators. You may start with...

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;

}

Solutions

Expert Solution

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:


Related Solutions

Modify the object provided in the code below to include a variety of overloaded operators in...
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...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >=...
1.) Modify your Student class to overload the following operators: ==, !=, <, >, <=, >= as member operators. The comparison operators will compare students by last name first name and id number. Also, overload << and >> as friend operators in the Student class. Overload << and >> operators in Roster class as well to output Rosters in a nice format as well as input Roster. Provide a function sort in a Roster class as a private function to...
Using Node.js as a Webserver 1. How do you modify a webserver.js code to include your...
Using Node.js as a Webserver 1. How do you modify a webserver.js code to include your name and date? Please include code. 2. Create a MongoDB database for the following group that includes member names and contact information and Include code: Gourp member exapmples: Pete 912-555-6666; Sam 912-111-3333; Mary 678-111-1111; and April 912-690-1111
You are being strangled! Why do you die? Include in your answer a description of the...
You are being strangled! Why do you die? Include in your answer a description of the need for O2 for each of the following processes and explain the specific consequences of O2 depletion on each. -glycolysis -the citric acid cycle -the electron transport chain -fermentation
Provide a description of our home galaxy, the Milky Way. Include in your description the location...
Provide a description of our home galaxy, the Milky Way. Include in your description the location and stellar populations (new stars/ old stars) of the central bulge, disk, and halo. Also provide details such as the Hubble classification, disk structure, location of star forming regions, and the best estimate of the number of spiral arms.
1.the object of a tax, which may include income, property, the value of goods sold is...
1.the object of a tax, which may include income, property, the value of goods sold is defined as the tax rate tax base tax activity tax fee 2. which of the following is a tax on wealth? income tax property tax sales tax excise tax 3. In judging the merits of a tax it is important to consider if the tax burden is justly distributed(equity) if the tax burden improves the allocation of resources it the tax is enforceable all...
Think of a BBQ in which you use charcoal to cook your food. You may start...
Think of a BBQ in which you use charcoal to cook your food. You may start with 10 pounds of charcoal. After you finish cooking the food, you may only have a small amount of ash left in the grill. What happened to the charcoal? Does the Law of Conservation of Mass apply to the burning of the charcoal? Clearly explain your position. Hint: Consider that the type of reaction between charcoal and oxygen in air and the identity of...
You may have heard the statement that you should not include your home as an asset...
You may have heard the statement that you should not include your home as an asset in your investment portfolio. Assume that your house will compromise up to 75% of your assets in the early part of your investment life. Evaluate the implications of omitting it when calculating the risk of your overall investment portfolio:
Analyze a start-up company you create. Include in your analysis the type of company you have...
Analyze a start-up company you create. Include in your analysis the type of company you have created, its business objectives, and other factors that you believe are important to the success of the business. 
In java Modify your finished guessNumber.java program to include a random number generator to generate the...
In java Modify your finished guessNumber.java program to include a random number generator to generate the guessed number (from 1 to 10). use the Random class to generate a random number between a range of integers. In your program include an if … then statement to check if the number you entered is bigger or smaller than the guessed number, and display a message. Also included is a counter to keep track on how many times the user has guessed,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT