C++ polynomial class
<p>Assignment: 
In this homework, you are going to develop a C++ class to describe and manipulate
polynomials.</p>

<p>complete the class declaration in the header and then implement (in a matching .cpp file)
the functions that are declared in the header file and test them to ensure they work as advertised.</p>

<p>The testing program supplied is very basic. You will be expected to modify it and add some (2 or more)
 tests to ensure that all aspects of the new class areexercised.</p>

<p>Example results from TestThePoly.cpp:</p>

<pre><code>(1) Testing cout &lt;&lt; A: empty
(2) Testing cin &gt;&gt; A':
Enter the polynomial (integer degree then double coefficients):
3 -1 2.09 -5.3 -0.98
(3) Second look at A: -1x^(3) +2.09x^(2) -5.3x^(1) -0.98
(4) Testing Polynomial B(A): -1x^(3) +2.09x^(2) -5.3x^(1) -0.98
(5) Testing Polynomial C(2, clist)': +1x^(2) +4.5x^(1) +8
(6) Testing D = C: +1x^(2) +4.5x^(1) +8
(7) Testing A == B : TRUE
(8) Testing A == D : FALSE
</code></pre>

<p>I have attempted to do this assignment many times but haven't figured it out. I looked online for something that I could help me but didn't find anything. Most of the other polynomial class problems seem to involve multiplication and such, and are at least a bit more complicated then this.  Or they are in some ways simpler/doing it differently then me. </p>

<p>Here is the header file</p>

<pre><code>// Polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include &lt;iostream&gt;
using std::ostream;
using std::istream;




class Polynomial
{

  public:
    Polynomial();
    Polynomial( int dgr, const double* clist );
    Polynomial( const Polynomial&amp; );
    ~Polynomial();

    int setDegree(int dgr); 
    int getDegree() const;   

    const Polynomial&amp; operator=( const Polynomial&amp; clist);
    bool operator==( const Polynomial&amp; clist) const;




    void printPolynomial( int dgr, const double* clist );


    double operator[]( int index ) const;
    double&amp; operator[]( int index );

  private:
      int    degree;
      double *coefficients; // list of coefficients


};

ostream&amp; operator&lt;&lt;( ostream&amp; , const Polynomial&amp; );
istream&amp; operator&gt;&gt;( istream&amp; , Polynomial&amp; );

#endif
</code></pre>

<p>Here is the source file </p>

<pre><code>// Polynomial.cpp


#include &lt;iostream&gt;
using std::istream;
using std::ostream;

using std::cerr;
using std::endl;

#include &lt;cstdlib&gt;
using std::exit;

#include "Polynomial.h"


Polynomial::Polynomial()
{
    degree        = 0;
    coefficients = 0;

}


Polynomial::Polynomial(int dgr, const double* clist)
{
     getDegree();

}

int Polynomial::getDegree() const
{
    return degree; 
}

Polynomial:: ~Polynomial()
{
    setDegree(0); 
}

int Polynomial::setDegree(int dgr)
{

    if( dgr &lt;0 ) {
    cerr &lt;&lt; "Error:  attempted to set a negative degree" &lt;&lt; endl;
    exit(1);  
  } else {
    if( coefficients!=0 ) {
      delete [] coefficients;
      coefficients = 0;
      degree       = 0;
    }
    if( dgr !=0 ) {
      coefficients = new double [dgr];
      if( coefficients==0 ) {
        degree =0;
        cerr &lt;&lt; "Warning:  unable to allocate enough space for list" &lt;&lt; endl;
        exit(2); // this surely is reasonable.
      } else {
        degree=dgr;
      }
    }
  }
  return dgr;


}

//copy constructor 
Polynomial::Polynomial(const Polynomial&amp; clist) : degree(0), coefficients(0)
{
  if( clist.getDegree()&lt;=0 ) {
    setDegree(0);
  } else {
    setDegree(clist.getDegree());
    for (int i=0; i&lt; degree ; i++) {
      coefficients[i]=clist.coefficients[i];
    }
  }
}


const Polynomial&amp; Polynomial :: operator=(const Polynomial&amp; clist) 
{
    if ( &amp;clist==this ) 
    {
    cerr &lt;&lt; "Warning:  attempt to copy Polynomial onto self" &lt;&lt; endl;
    } 
    else {
    if( clist.getDegree()&lt;=0 ) {
      setDegree(0);
    } else {
      setDegree(clist.getDegree());
      for (int i=0; i&lt; degree ; i++) {
        coefficients[i] = clist.coefficients[i];
      }
    }
  }
  return *this;
}

bool  Polynomial::operator==(const Polynomial&amp; clist) const
{
    bool result=true;
  if( degree!=clist.getDegree() ) {
    result=false;
  } else {
    for(int i=0; i&lt; degree &amp;&amp; result == true; i++) {
      result = coefficients[i]==clist.coefficients[i];
    }
  }
  return result;
}


double Polynomial::operator[]( int index ) const
{
  if( index&lt;0 || index&gt;= degree ) {
    cerr &lt;&lt; "Attempt to access element outside index bounds" 
      &lt;&lt; endl;
    exit(3); // Maybe not reasonable. 
  } else {
    return coefficients[index];
  }
}


double&amp; Polynomial::operator[]( int index )
{
  if( index &lt; 0 || index&gt;= degree  ) {
    cerr &lt;&lt; "Attempt to access element outside index bounds" 
      &lt;&lt; endl;
    exit(4); 
  } else {
    return coefficients[index];
  }
}



ostream&amp; operator&lt;&lt;( ostream&amp; left, const Polynomial&amp; right)
{
  if( right.getDegree() &gt; 0 ) {
    left &lt;&lt; endl;
    for (int i=0; i&lt; right.getDegree(); i++ ) {
    for (int j = 0; j &gt;= 0 ; j -- ){


      left &lt;&lt; right[i] &lt;&lt; "X^(" &lt;&lt; j &lt;&lt; ") " ;
  if (j &gt; 0) 
  {     
      left &lt;&lt; " + ";
  }

      }
    }
  } 
  else {
    cerr &lt;&lt; "Warning:  Attempt to display empty List" &lt;&lt; endl;
  }


  return left;
}


istream&amp; operator&gt;&gt;( istream&amp; left, Polynomial&amp; right)
{
  int tmp;
  left &gt;&gt; tmp;
  if( tmp&lt;=0 ) {
    cerr &lt;&lt; "First value expected to be list size, &gt; 0." &lt;&lt; endl;
  } else {
    if( tmp!=right.getDegree() ) {
      right.setDegree(tmp);
    }
    for (int i=0; i&lt;right.getDegree(); i++) {
      left &gt;&gt; right[i];
    }
  }
  return left;
}
</code></pre>

<p>Here is the testing/main file </p>

<p>// Prog11.cpp</p>

<pre><code>#include &lt;iostream&gt;
using std::cout;
using std::endl;
using std::cin;

#include "Polynomial.h"


int main()
{
  Polynomial A;
  cout &lt;&lt; "(1) Testing `cout &lt;&lt; A':  " &lt;&lt; A &lt;&lt; endl;

  cout &lt;&lt; "(2) Testing `cin &gt;&gt; A':\n";
  cout &lt;&lt; "Enter the polynomial (integer order then double    coefficients):\n\t ";
  cin &gt;&gt; A;
  cout &lt;&lt; endl;
  cout &lt;&lt; "(3) Second look at A:  " &lt;&lt; A &lt;&lt; endl;

  Polynomial B(A);
  cout &lt;&lt; "(4) Testing `Polynomial B(A)':  " &lt;&lt; B &lt;&lt; endl;

  double clist[]={8, 4.5, 1};
  Polynomial C(2, clist);
  cout &lt;&lt; "(5) Testing `Polynomial C(2, clist)':  " &lt;&lt; C &lt;&lt; endl;

  Polynomial D=C;
  cout &lt;&lt; "(6) Testing D = C):  " &lt;&lt; D &lt;&lt; endl;

  cout &lt;&lt; "(7) Testing A == B :  " &lt;&lt; (A==B ? "TRUE" : "FALSE") &lt;&lt; endl;
  cout &lt;&lt; "(8) Testing A == D :  " &lt;&lt; (A==D ? "TRUE" : "FALSE")&lt;&lt; endl;

  return 0;
}
</code></pre>