Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Develop the class Polynomial. The internal representation of a Polynomial

ID: 3823074 • Letter: I

Question

In C++ Develop the class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term: 2x^4 has the coefficient of 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities: Overload the addition operator (+) to add two Polynomials. Overload the subtraction operator () to subtract two Polynomials In C++ Develop the class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent. The term: 2x^4 has the coefficient of 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities: Overload the addition operator (+) to add two Polynomials. Overload the subtraction operator () to subtract two Polynomials 2x^4 has the coefficient of 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities: Overload the addition operator (+) to add two Polynomials. Overload the subtraction operator () to subtract two Polynomials

Explanation / Answer

#include #include #include struct Term { int coeff; unsigned exp; Term(int c, unsigned e) : coeff(c), exp(e) {}; }; class Polynomial { public: Polynomial() = default; Polynomial(const Polynomial&) = default; Polynomial(const std::vector& c) : coeff(c) {}; Polynomial(const Term t); Polynomial(const std::vector& t); std::string print() const; Polynomial& operator=(const Polynomial& other) = default; Polynomial& operator-=(const Polynomial& other); Polynomial& operator+=(const Polynomial& other); private: std::vector coeff; }; Polynomial::Polynomial(const Term t) : coeff(t.exp + 1) { coeff[t.exp] = t.coeff; } Polynomial::Polynomial(const std::vector& t) { for(auto term: t) *this += term; } std::string Polynomial::print() const { std::string temp; for(signed long long i = coeff.size() - 1; i >= 0; --i) { if (coeff[i] == 0) continue; if (temp != "") temp += " + "; if (coeff[i] != 1 || i == 0) temp += std::to_string(coeff[i]); if (i != 0) temp += "x^" + std::to_string(i); } return temp; } Polynomial& Polynomial::operator+=(const Polynomial& other) { if(coeff.size()