what needed is 1 Modify the code so that you overload the
what needed is : 1- Modify the code so that you overload the \"-\" operator to subtract array2 from array1 2- Overload the \"<<\" operator so that it will print the entire array on one line 3- if you complete the mine pkease do it #include <iostream> using namespace std; #define MAXARRAY 5 class myarray { private: int value[MAXARRAY]; public: void setvalue(int index, int newvalue){ value[index]=newvalue;} int getvalue(int index){ return value[index];} myarray operator+(myarray array2) { myarray temparray; for (int i=0; i<MAXARRAY; i++) temparray.value[i]=value[i] + array2.value[i]; return temparray; } }; int main () { myarray array1, array2, array3; int i; //INITIALIZE for (i=0; i<MAXARRAY; i++) { array1.setvalue(i,i); array2.setvalue(i,i+3); } //ADD array3=array1 + array2; //PRINT cout << \"array1 array2 array3\" << endl; for (i=0; i<MAXARRAY; i++) cout << array1.getvalue(i) << \" \" << array2.getvalue(i) << \" \" << array3.getvalue(i) << endl; return 0; } Solution
Here is the update of the class with specified overloaded operators for you:
#include <iostream>
using namespace std;
#define MAXARRAY 5
class myarray
{
private:
int value[MAXARRAY];
public:
void setvalue(int index, int newvalue){
value[index]=newvalue;}
int getvalue(int index){
return value[index];}
myarray operator+(myarray array2)
{
myarray temparray;
for (int i=0; i<MAXARRAY; i++)
temparray.value[i]=value[i] + array2.value[i];
return temparray;
}
myarray operator-(myarray array2)
{
myarray temparray;
for (int i=0; i<MAXARRAY; i++)
temparray.value[i]=value[i] - array2.value[i];
return temparray;
}
friend ostream& operator<<(ostream& os, const myarray& m);
};
ostream& operator<<(ostream& os, const myarray& m)
{
for(int i = 0; i<MAXARRAY; i++)
os << m.value[i] << \" \";
os << endl;
return os;
}
int main ()
{
myarray array1, array2, array3;
int i;
//INITIALIZE
for (i=0; i<MAXARRAY; i++)
{
array1.setvalue(i,i);
array2.setvalue(i,i+3);
}
//ADD
array3=array1 + array2;
//PRINT
cout << \"array1 array2 array3\" << endl;
for (i=0; i<MAXARRAY; i++)
cout << array1.getvalue(i) << \" \"
<< array2.getvalue(i) << \" \"
<< array3.getvalue(i) << endl;
return 0;
}

