1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ assignment involving classes

  1. Mar 12, 2005 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Ugh! I'm working on my last homework assignment and I can't seem to even get off the ground. :cry:
    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
    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
     
  2. jcsd
  3. Mar 12, 2005 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  4. Mar 12, 2005 #3

    saltydog

    User Avatar
    Science Advisor
    Homework Helper

    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: Mar 12, 2005
  5. Mar 12, 2005 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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
     
  6. Mar 12, 2005 #5

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
  7. Mar 12, 2005 #6

    saltydog

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  8. Mar 12, 2005 #7

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    cheers, SD! this helps a lot.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ assignment involving classes
  1. Interesting assignment (Replies: 4)

  2. Kinematics Assignment (Replies: 2)

  3. Assignment on friction (Replies: 1)

  4. Springs Assignment (Replies: 11)

Loading...