Tom Kelliher, CS18
May 6, 1996
Polynomial Class Declaration:
#ifndef __POLYNOMIAL_H #define __POLYNOMIAL_H #include "list.h" class Polynomial { private: List L; int degree; public: Polynomial(void) : degree(-1) {} ~Polynomial() {} // Ensure that L's copy constructor is called. Polynomial(const Polynomial& b) : L(b.L) {} const Polynomial& operator=(const Polynomial& b) { L = b.L; } int Degree(void); int SetCoefficient(int exp, double co); int RetrieveCoefficient(int exp, double& co); void Print(void); const Polynomial operator+(const Polynomial& b) const; const Polynomial operator-(const Polynomial& b) const; const Polynomial operator*(const Polynomial& b) const; }; #endif
Might be useful to define some private member functions.
Recall the list member functions:
List(void) : l(NULL) {} ~List(); List(const List& b); const List& operator=(const List& b); int Insert(int exp, double co); int Retrieve(int exp, double& co); int Delete(int exp); void Print(void); // For testing.
Polynomial P; P.SetCoefficient(2, 1.33); P.SetCoefficient(2, 4.0);What list operations need to be performed?
2.3X^3 + -6.3X + 7.1
Need a local polynomial.
What should it return?
How is addition performed?
How the function should look:
const Polynomial Polynomial::operator+(const Polynomial& b) const { Polynomial Sum; // Code for addition, using operations like: // degree = (Degree() > b.Degree()) ? Degree() : b.Degree(); // check1 = Retrieve(i, co1); // check2 = b.Retrieve(i, co2); return Sum; }
How do we multiply?