-2

I am expected to create a simple c++ program that have a class named polynomial. It will create dynamic array has 6 data in it and every ones index is exponent of the term and its value is coefficient. Everything is good till I face with that: "a member function overloaded + operator (prefix) which will find and return the derivative of the polynomial ".

There,I really could not understand what expected from me to do.In constructor I set my array size to 6 as I will be used. For example; while run-time ,I will command the polynomial like:7.4x^5+3.1x^2-10.2x+14.9 and it will give me the derivative of it. Exactly what I am asking that not the all codes down but the logic with that + operator overloading.Apart from it I have no problem with my program.

2 Answers2

0

Supposed that your class is named Polynomial, you can overload the prefix increment operator like below:

class Polynomial {
...
public:
...
  Polynomial& operator++()
  {
  /* transform the polynomial to its derivative here */
  /* the new polynomial will have -1 coefficients from the original */
  return *this;
  }
...
};

then in code you can do the following:

Polynomial poly(...);
++poly;

HTH

101010
  • 41,839
  • 11
  • 94
  • 168
  • Thanks It made sense.The trouble maker that made assignment probably forgot second "plus" in that,and I thing it does not matter where the operator overloaded is ++ or + or anything else. – user3578573 May 20 '14 at 19:57
  • 2
    I would not modify the `this` object, I would return the derivative polynomial by value. – aschepler May 20 '14 at 19:58
0

With what we have been able to gather from the comments, what you need is to provide a member overload of the unary + operator. This operator works as such:

Polynomial poly = Polynomial( /*initialise*/ );
Polynomial derivative;
derivative = +poly;

So in words, it acts on a polynomial and returns new polynomial that is its derivative.

The requirement is to declare it as a member of your class, so that means that you need to add the following to your class definition (as a public member):

Polynomial operator+();

Then in your source file you'll need to implement it; the skeleton for this code:

Polynomial Polynomial::operator+()
{
    Polynomial derivative;

    /* TODO: Set the derivative's coefficients to the derived coefficients of this */

    return derivative;
}

Now this code requires you to have a copy constructor implemented. If you don't have this yet, or don't know how to do this, and you can't find the answer on your own then please post a new question. Keep in mind the rule of three.

MicroVirus
  • 5,324
  • 2
  • 28
  • 53