// Polynomial class, version 1. Publically, this version and the // other have the same interface. The class represents an individual // polynomial. It provides member functions to read a polynomial, // evaluate a polynomial, assign one polynomial to another, add two // polynomials, multiply two polynomials, and output a polynomial. // // The internal implementation of the two versions are different. // This one represents coeffients of all terms, even those that are // zero, up to a highest degree. This degree can be increased by the // assign_coefficient function. The various operators are designed // to make use of this representation. // // The implementation given here goes beyond the design given in the // text. One important way is that there is no artificial limit on // the size of the exponent. // #ifndef POLYNOMIAL_H #define POLYNOMIAL_H #include using namespace std; class Polynomial { public: // Constructors and destructors. Polynomial( ); Polynomial( double a0 ); // convert a constant to a polynomial Polynomial( const Polynomial & old ); ~Polynomial( ); // Assignment operator. const Polynomial & operator = ( const Polynomial& right ); // Evaluate the polynomial at the given value. double operator() ( double x ) const; // Access double get_coefficient( int exponent ) const; int max_degree() const; // Modification void assign_coefficient( double coefficient, int exponent ); // A few of the many operators we would want to provide. These are // ok as friend functions because compiler issues don't generally // arise when the class is not templated. friend Polynomial operator+ (const Polynomial& f, const Polynomial& g); friend Polynomial operator- (const Polynomial& f, const Polynomial& g); friend Polynomial operator* (const Polynomial& f, const Polynomial& g ); friend Polynomial operator* (double a, const Polynomial& f); friend Polynomial operator* (const Polynomial& f, double a); // Output operator as a friend function. friend ostream& operator<< ( ostream& ostr, const Polynomial& f ); private: vector coefficients_; }; #endif