Question

In: Computer Science

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 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;
  
};

Solutions

Expert Solution



// 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


Related Solutions

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...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. use OPTIONAL TYPE and NULL object design pattern.   import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map aTags = new HashMap<>(); public Student(String pName) { aName =...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the class's capabilities in a simple program. this is what needs to be modified // Specification file for the FeetInches class #ifndef FEETINCHES_H #define FEETINCHES_H #include <iostream> using namespace std; class FeetInches; // Forward Declaration // Function Prototypes for Overloaded Stream Operators ostream &operator << (ostream &, const FeetInches &); istream &operator >> (istream &, FeetInches &); // The FeetInches class holds distances or measurements...
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...
Python: How would I modify the class below that takes a string and returns an object...
Python: How would I modify the class below that takes a string and returns an object holding a valid NANP phone number. I am asked to filll in the three methods listed, but underfined, below: __str__(), area_code(), and normalize(). My task is to clean up differently formatted telephone numbers by removing punctuation, such as '(', '-', and the like, and removing and the country code (1) if present. I am asked to start by stripping non-digits, and then see if...
Below are the control strategy of hydraulic hybrid vehicle. How can I modify code below to...
Below are the control strategy of hydraulic hybrid vehicle. How can I modify code below to include the torque of motor of hydraulic hybrid vehicle? and How can I improve this function [SOC,k,T_engine,S_engine,T_brake,T_pump] = strategy(duration,gamma,P,V,Pmin,Pmax,Vmin,Vmax,SOC,Disp,T_wheel,S_wheel,gearratio,S_map,Te_max,T_engine,S_engine,T_pm,k,eff_mech,eff_hyd) S_flywheel = S_wheel*gearratio; T_flywheel = T_wheel/gearratio; T_brake = 0; if SOC < 0.1 k=1; %Engine on elseif SOC > 0.7 k=0; %Engine off end %T_pump +ve = charging %T-pump -ve = discharging if k==1 if T_engine*eff_mech < T_flywheel    %Engine provides full torque when hydraulic...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT