# overloading operators..........subtraction problem

Printable View

• 05-24-2011
zamzamation
overloading operators..........subtraction problem
hello everyone :)
I'm making a class that has the subtraction operator overloaded
i subtract the 2 values in 2 diff arrays and put them in a new array
the problem now is it doesn't subtract the 2 values at all ~_~, just puts the values in a[i] to result.a[i] .........
umm Why?

Code:

```Poly Poly::operator -(Poly x) {         Poly Result;         for(int i=0;i<7;i++)                 Result.a[i]=a[i]-x.a[i];         return Result; }```
• 05-24-2011
tabstop
Because it doesn't. Why do you believe that it does? (I.e., the error may be in your print routine.)
• 05-24-2011
zamzamation
ok am new to programming still a student xD
this is the full code till now
Code:

```#include<iostream> using namespace std; class Poly {         friend void print_Poly(Poly r);//prints poly protected:         float coef;         int expo;         float a[7]; public:         Poly();//constructor for zeros         float get_co(int x);//gets coef from array using expo         int get_exp(float y);//gets expo from array using coef         float set_co_expo(int x,float y);//sets coef and expo in array         Poly operator + (Poly x);//overloads add         Poly operator - (Poly x);//overloads subtraction         bool operator == (Poly x) ;//overloads assigning         Poly operator += (Poly x) ;//overloads +=         Poly operator -=(Poly x);//overloads -= }; void print_Poly(Poly r) {         for(int i=0;i<7;i++)         {                 cout<<r.get_co(i)<<"X"<<"^"<<i<<" + ";         };         cout<<"0"<<endl; } Poly::Poly() {         coef=0;         expo=0;         for(int i=0;i<7;i++)                 a[i]=0; } float Poly::get_co(int x) {         return a[x]; } int Poly::get_exp(float y) {         for(int i=0;i<7;i++)         {                 if(a[i]==y)                         return i;         }; } float Poly::set_co_expo(int x,float y) {         expo=x;         coef=y;         a[expo]=y;         return a[expo]=y; } Poly Poly:: operator +(Poly x) {         Poly Result;         for(int i=0;i<7;i++)                 Result.a[i]=a[i]+x.a[i];         return Result; }; Poly Poly::operator -(Poly x) {         Poly Result;         for(int i=0;i<7;i++)                 Result.a[i]=a[i]-x.a[i];         return Result; } bool Poly::operator ==(Poly x) {         for(int i=0;i<7;)         {                 if(a[i]==x.a[i])                 {                         cout<<"matching"<<endl;                         return true;                 }                 else                 {                         cout<<"not matching"<<endl;                         return false;                 }                 i++;         }; } Poly Poly::operator +=(Poly x) {         Poly Result;         for(int i=0;i<7;i++)         {                 a[i]=a[i]+x.a[i];                 Result.a[i]=a[i];         }         return Result; } Poly Poly::operator -=(Poly x) {         Poly Result;         for(int i=0;i<7;i++)         {                 a[i]=a[i]-x.a[i];                 Result.a[i]=a[i];         }         return Result; } int main() {         Poly test,ram;         ram.set_co_expo(5,1);         test.set_co_expo(5,1.5);         test.set_co_expo(3,40);         print_Poly(test);         print_Poly(ram);         print_Poly(test.operator+(ram));         test.operator==(ram);         print_Poly(test.operator+=(ram));         print_Poly(test.operator-(ram));         print_Poly(test.operator-=(ram));         return 0; }```
• 05-24-2011
tabstop
So you should print out test (1.5x^5+40x^3), ram (x^5), two copies of test+ram (2.5x^5+40x^3), and then two copies of the original value of test again. At no point do you even get to a point where you are subtracting a larger number from a smaller number.
• 05-24-2011
zamzamation
wait a sec do u mean the print function prints test itself not the Poly returned by the function? 0_0
• 05-24-2011
tabstop
Quote:

Originally Posted by zamzamation
wait a sec do u mean the print function prints test itself not the Poly returned by the function? 0_0

Well, when you write "print_Poly(test)" what else did you expect it to print?

As to the others, of course it prints the poly returned by the function, which is why it prints two copies of test+ram, and then two copies of the original value of test (because when you get to that point, you've added ram to test, so test has changed, but you're subtracting ram which means the thing you are printing is back to the original value).
• 05-24-2011
zamzamation
oh about the print_Poly(test)---> I know that
i just forgot that the value of test has changed already LOL
seriously thx alot :)
woow i don't know how did i forget that the value changed already lol
thx again alot :)
• 05-24-2011
iMalc
Your operator -= and += are unnecesarily returning a copy of the item being modified whereas they should just be returning a reference to *this and the Result variable can be removed.