Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Polynomial Program and Overloaded Operators...Please Help! 1

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hello everybody. I have a program to write, and I am having trouble with it. Can someone take a look at it and write out a possible code for it? i would really appreciate it. Thanks, here it is:

The program will develop a polynomial class. how do you represent a polynomial using an array and with its overloaded operators?
Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term 2x to the 4th power has a coefficient of 2 and an exponent of 4. Develop a full calss containing proper constructor and destructor functions as well as set and get functions. the class should also provide the following overloaded operator capabilities:
a) Overlaoad the addition operator (+) and add two Poly's
b) Overlaod the sub operator (-) adn sub two Poly's
c) Overload the assignment operator to assign one Poly to another
d) Overload the mult operator to mult two Poly's
e) Overload the add assignment operator (+=), the sub assignment operator (-=), and the mult assignment operator (*=).


If anyone can help, please give me some suggestions. Thanks again.

DonCL
 
I assume this will not be using multi-variable calculus.
i.e. 2x + 3y = 4. With that in mind I am going to assume that x is the only variable.

class polynomial{
public:
polynomial operator + (polynomial p)
{
if(p.exponent == exponent)
// you can add the 2 coefficents
else
// i dont know what you want to do here
}

// subtraction see above

polynomial operator * (polynomial p)
{

// multiply the coefficients
// add the exponents
}

};



This should get you started


matt
 
So do I just repeat the same code with the different operators until the end? also, do i return 0? thanks for the help.
 
Yes you repeat the code adjusting for the different opperations. on the +=, *= etc you will be setting the values of this->coefficient and this->exponent. Returning zero is not what you want to do. In the case of a regular + i would return a polynomial that will not go out of scope after returning it. In all of these i would return a polynomial. If you get stuck i can email you a snip of code that will show you what I mean.

Matt
 
That would be really great if you can email me the code of what you are talking about. Thanks a lot for your help.
 
oh, im sorry. my email is cl2001@optonline.com. thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top