Homework Help: C++ assignment involving classes

1. Mar 12, 2005

Math Is Hard

Staff Emeritus
Ugh! I'm working on my last homework assignment and I can't seem to even get off the ground.
The teacher created a class for rational numbers, and now we are working on some functions for a polynomial class which uses arrays of rationals from his class. My polynomials are arrays of coefficients with each stored at an index position representing the power of x the coefficient is multiplied to.
I'm embarrassed to ask for help because there are 4 files involved: the rational class header and implementation file and the polynomial class header and implementation file.
I know you people have better things to do this weekend, but if anyone is really bored, the assignment and files are here:
http://www.math.ucla.edu/~rclark/10a.1.05w/hw6/hw6.html [Broken]
It's the polynomial.cpp file where we have to fill in the blanks.
One of the first things I tried to do was the operator overloading to define addition for polynomials. Here's what I tried:
Code (Text):

// operator+
polynomial operator+(const polynomial & a,const polynomial & b)
{
polynomial c;
for (int num = 0; num < polynomial::SIZE; num ++) //fill array with zeros
c[num] = 0;
for (int i = 0; i < polynomial::SIZE; i ++) //compute sum
c[i] = a[i] + b[i];
return c;
}

So far, not so good. It would be a huge help if someone could help me get just this piece fixed up. Thanks,
MIH

Last edited by a moderator: May 1, 2017
2. Mar 12, 2005

chroot

Staff Emeritus
Why does the class's name not appear in the declaration of the function? Does this function appear in the header, inside the class declaration, or in the implementation file?

- Warren

3. Mar 12, 2005

saltydog

The polynomial class has a data member coeff that holds the coefficients. Thus, need to use the construct:

c.coeff=a.coeff+b.coeff;

Right?

Oh yea, be nice if you could run this through a debugger. Microsoft Visual C++ has a nice one.

Last edited by a moderator: May 1, 2017
4. Mar 12, 2005

chroot

Staff Emeritus
MIH,

In general, please tell also provide us the compiler (and linker) output when asking questions like this. It's really hard (if not impossible) to diagnose a piece of code like this taken from a larger piece of unknown code. You didn't even tell us why it was "not so good."

- Warren

5. Mar 12, 2005

Math Is Hard

Staff Emeritus
ok, sorry. I am using MS VC++ v 6. The whole thing (all four files) is at the link I listed in my OP. The class name is polynomial. It's defined in polynomial.h.
Saltydog - yes I believe that's the problem I was having. It was not using that coeff data member. That's where I was screwing up, I bet.
I wasn't sure if it was ok for me to use the brackets [], because I was getting errors (among the jillions of other errors) about [ not being defined. I felt so lost I didn't even know how to describe the problem I was having because there were so many.
Thanks so much for looking at that. I'll go work on this some more.

6. Mar 12, 2005

saltydog

You got that error because using the construct c implies an "array" of polynomials which you did not define. Use the same c.coeff construct for operator-, a little more complicated looping for operator*, and some if-statements for overloading the output operator and return "os" after you've filled it up with the polynomial.

7. Mar 12, 2005

Math Is Hard

Staff Emeritus
cheers, SD! this helps a lot.