title: Beginner polynomial program in C++
body: <p>I'm looking for some assistance on an exercise for my C++ programming class. Unfortunately, I was rather ill this week and unable to attend class, meaning I have only been able to use the textbook as a resource, so I'm pretty lost when it comes to writing this program. Therefore, I figured I'd turn to the real pros here for assistance. I realize this is broad, but any help and/or tips would be appreciated. Here are the instructions we were given:</p>
  
  <blockquote>
    <p>This assignment deals with representing and manipulating polynomials using simple arrays. A
    polynomial, such as anxn + an-1xn-1 + … + a0, will be implemented as an array of coefficients, with >coefficient ai being stored in location i of the array. The coefficients are floating point values
    (potentially negative), so we will use an array of type double. The array will be of size <em>MAXPOLY</em>
    (a constant variable set to 50) and so we will be limited to holding polynomials with maximum
    degree of <em>MAXPOLY</em> – 1 (or 49).<br><br>
    The file <strong>Poly.h</strong> describes all the functions provided by the class. <br><br>
    You are to implement the following set of functions:<br>
     - Default constructor to initialize a polynomial to the zero
       polynomial<br>
     - <em>setCoeff</em> to set a specific coefficient in the polynomial<br>
     - <em>retrieveCoeff</em> to get a specific coefficient from the polynomial<br>
     - <em>incrementCoeff</em> to add a value to a specific coefficient in the polynomial<br>
     - <em>degree</em> which determines the degree of the polynomial<br>
     - <em>numOfTerms</em> which determines the number of terms in the polynomial (i.e., how many array elements are nonzero)<br>
     - <em>evaluate</em> which evaluates the polynomial for a given value of X<br>
     - <em>add</em> which adds one polynomial to another, changing the polynomial added to<br>
     - <em>derivative</em> which computes the derivative of a polynomial<br>
     - <em>equals</em> which determines the equality of two polynomials<br></p>
    
    <p>Several functions are provided for you: (1) a <em>toString</em> function will be provided to you so that >all our polynomials will be displayed identically, (2) the insertion operator is defined so we can >easily print a polynomial, and (3) the equality, inequality, and addition operators are provided >and are simply defined in terms of your equals and add functions. You should not change any of the provided functions.</p>
    
    <p>You will be supplied two starter files, <strong>Poly.cpp</strong> and <strong>Poly.h</strong>. The class declaration
    file Poly.h contains a complete specification of a class named Poly. Your task will be to implement >all the specified functions in the class definition file Poly.cpp (with the exception of the few >functions which have been provided for you.). You have also been provided with an initial test >program PolyTest.cpp. You should add code to the PolyTest.cpp file to fully test your Poly class(copy code from the PolyTest.cpp file you created for Project #1-Pre).</p>
  </blockquote>
  
  <p>We were, indeed, supplied those files. The <strong>Poly.h</strong> file looks like this:</p>
  
  <pre><code>#define POLY_H
  #ifndef POLY_H
  #include &lt;string&gt;
  using namespace std;
  
  const size_t MAXPOLY = 50;    
  
  class Poly
  {
  
  private:
      // Data members   [implementation of ADT's data object]
  
      // array for holding the coefficients of the poly
      double coeff[MAXPOLY];               
  public:
  
      // Default Class constructor: initializes a polynomial to the constant 0
      // note: all array elements of coeff[] must be set to 0.0  
      Poly ();
  
      // degree: finds the degree of a polynomial (the highest power with a non-zero coefficient)
      size_t degree () const;
  
      // setCoeff: sets a term, value*x^i, in a polynomial
          // Throws &lt;std::out_of_range&gt; if index i does not meet the precondition.
      void setCoeff (double value, size_t i);
  
      // retrieveCoeff: finds the coefficient of the x^i term in poly
      // Throws &lt;std::out_of_range&gt; if index i does not meet the precondition.
      double retrieveCoeff (size_t i) const;
  
      // incrementCoeff: changes a term, value*x^i, in a polynomial
      // Throws &lt;std::out_of_range&gt; if index i does not meet the precondition.
      void incrementCoeff(double value, size_t i);
  
      // toString: produce a string representation of a Poly object
      // note: This function has been provided for you -- DO NOT CHANGE IT!
      string toString() const;
  
      // numOfTerms: returns the number of terms in the polynomial.
      size_t numOfTerms () const;
  
      // evaluate: evaluate a polynomial for a specified value of X
      double evaluate (double x) const;
  
      // add: add one polynomial to another
      void add (const Poly&amp; aPoly);
  
      // addition operator: add two polynomials together and return a new polynomial that is the result
      // note: This function has been provided for you -- DO NOT CHANGE IT!
      Poly operator+ (const Poly&amp; rhs) const;
  
      // equals: determine if two polynomials are equal
      bool equals (const Poly&amp; aPoly) const;
  
      // Equality/inequality operators
      // note: These functions have been provided for you -- DO NOT CHANGE IT!
      bool operator== (const Poly&amp; rhs) const;
      bool operator!= (const Poly&amp; rhs) const;
  
      // derivative: compute the derivative of a polynomial
      void derivative ();
  
      // insertion operator for output
      // note: This function has been provided for you -- DO NOT CHANGE IT!
      friend ostream&amp; operator&lt;&lt; (ostream&amp; os, const Poly &amp;p);
  };  
  
  #endif
  </code></pre>
  
  <p>The <strong>Poly.cpp</strong> file looks like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;stdexcept&gt;
  #include &lt;cmath&gt;
  #include "Poly.h"
  using namespace std;
  
  
  // Class constructor
  Poly::Poly ()
  {
      //ADD YOUR CODE HERE
  }
  
  // degree
  size_t Poly::degree() const
  {
      //ADD YOUR CODE HERE
  }
  
  // setCoeff
  void Poly::setCoeff (double value, size_t i)
  {
      // ADD YOUR CODE HERE
  }
  
  // retrieveCoeff
  double Poly::retrieveCoeff (size_t i) const
  {
      return 0;    // REPLACE WITH YOUR CODE
  }
  
  // incrementCoeff
  void Poly::incrementCoeff(double value, size_t i)
  {
      // ADD YOUR CODE HERE
  }
  
  // toString
  string Poly::toString() const
  {
      ostringstream result;
      bool printedSomething = false;
      for (int i=(int)degree(); i&gt;=0; i--) 
      {
            double c = retrieveCoeff(i);
            if (c != 0.0) 
        {
            printedSomething = true;
            if (i == 0) 
        {
                result.setf(ios::showpos);
                result &lt;&lt; " " &lt;&lt; c;
                result.unsetf(ios::showpos);
            }
            else 
        {
                result.setf(ios::showpos);
                result &lt;&lt; " " &lt;&lt; c;
                result.unsetf(ios::showpos);
                result &lt;&lt; "*X^" &lt;&lt; i;
            }
            }
        }
      if (!printedSomething) 
      {
          result.setf(ios::showpos);
          result &lt;&lt; " " &lt;&lt; 0;
          result.unsetf(ios::showpos);
      }
      return result.str();
  }
  
  
  // numOfTerms
  size_t Poly::numOfTerms () const
  {
      return 0;   // REPLACE WITH YOUR CODE
  }
  
  // evaluate
  double Poly::evaluate (double x) const
  {
      return 0;   // REPLACE WITH YOUR CODE
  }
  
  // add
  void Poly::add (const Poly&amp; aPoly)
  {
      // ADD YOUR CODE HERE
  }
  
  // addition operator
  Poly Poly::operator+ (const Poly&amp; rhs) const
  {
      Poly result;
      result.add(*this);
      result.add(rhs);
      return result;
  }
  
  // equals
  bool Poly::equals (const Poly&amp; aPoly) const
  {
      return false;   // REPLACE WITH YOUR CODE
  }
  
  // Equality/inequality operators
  bool Poly::operator== (const Poly&amp; rhs) const
  {
      return equals(rhs);
  }
  
  bool Poly::operator!= (const Poly&amp; rhs) const
  {
      return !equals(rhs);
  }
  
  // derivative
  void Poly::derivative ()
  {
      // ADD YOUR CODE HERE
  }
  
  // Friend operator for printing a Poly object.
  ostream &amp; operator &lt;&lt; (ostream &amp;out, const Poly&amp; p)
  {
      out &lt;&lt; p.toString();
      return out;
  }
  
  #endif
  </code></pre>
  
  <p>Although I have a basic understanding of C++, this is only the second week of classes (apparently a bad one to miss), so I am still in the learning stages. Any help, even if it is just a place to start, would be greatly appreciated. Thank you!</p>
  
  <p><strong>Note:</strong> I am compiling in Microsoft Visual Studio if that is of any help</p>