Design and implement a class for polynomials a n x n + a n-1 x n-1 + ... + a 1 x
ID: 3767532 • Letter: D
Question
Design and implement a class for polynomials anxn + an-1xn-1 + ... + a1x + a0 with integer coefficients. The coefficients and the degree are data members. The class operations should include addition, subtraction, multiplication and evaluation of a polynomial. Overload +, -, and * and implement the evaluation as a member function with an int argument. The evaluation member function returns the value obtained by plugging in its argument for x and performing the indicated operations. Include a default constructor, a copy constructor, and a constructor that takeas two arguments: a pointer to int and an int (this is the dynamic array). Include a suitable destructor and suitable functions to input and output polynomials by overloading << and >>. Write a driver to test all capabilities.
Explanation / Answer
class Polynomial { public: Polynomial(int theA,int theN,Polynomial* theLink) : a(theA),n(theN),link(theLink){}; int getA() const { return a; } int getN() const {return n; } Polynomial* getLink() const { return link; } void setA(int theA){a = theA;} void setN(int theN){n = theN;} void setPol(int theA,int theN){a = theA;n = theN;} void setLink(Polynomial* theLink){link = theLink;} Polynomial operator + (Polynomial* & object); Polynomial operator - (Polynomial* & object); Polynomial operator * (Polynomial* & object); friend ostream& operator > (istream& in,Polynomial& object); virtual ~Polynomial(); private:int a; int n; Polynomial* link; }; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.