Thread: overloading operators..........subtraction problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    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;
    }
    Last edited by zamzamation; 05-24-2011 at 07:36 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because it doesn't. Why do you believe that it does? (I.e., the error may be in your print routine.)

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    6
    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;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    6
    wait a sec do u mean the print function prints test itself not the Poly returned by the function? 0_0

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zamzamation View Post
    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).

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    6
    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

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overloading operators
    By thracian in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2008, 07:23 PM
  2. Overloading Operators
    By Zoalord in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2003, 09:08 AM
  3. Overloading Operators
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2002, 02:23 PM
  4. About overloading operators.
    By Dual-Catfish in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2001, 07:12 PM
  5. Overloading Operators
    By Raven Arkadon in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2001, 10:24 AM