Example 2: Polynomials Develop class Term. The class contains (a) Acoefficient,
ID: 3821120 • Letter: E
Question
Example 2: Polynomials Develop class Term. The class contains (a) Acoefficient, and an exponent. E.g., term 2x 4 has the coefficient of 2 and the exponent 4. (2) overload the output operator (s) to properly display the term Develop class Polynomial. The internal representation of a Polynomial is an array of terms. 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: b) Overload the assignment operator to assign one Polynomial to another. c) overload the addition assignment operator subtraction assignment operator Write a driver program. You feel free to define any Polynomial objects, and demonstrate you can use the operator overloading functions. Q003 Prentice Hall, Inc. All rights reserved. TTExplanation / Answer
Term.h
class Term
{
public:
Term(int coefficient, int exponent);
private:
int coefficient = 0;
int exponent = 0;
public:
void setCoefficient(int coefficient);
int getCoefficient();
void setExponent(int exponent);
int getExponent();
friend ostream& operator << (ostream& os, const Term& t);
void operator = (const Term &t );
Term operator + (const Term& b);
Term& operator += (const Term& rhs);
Term& operator -= (const Term& rhs);
};
Here, the function is declared as a friend of the class so that the std::osteam object can access its private data.
Term.cpp
#include "Term.h"
#include <iostream>
using namespace std;
Term::Term(int coefficient, int exponent)
{
this->coefficient = coefficient;
this->exponent = exponent;
}
void Term::setCoefficient(int coefficient)
{
this->coefficient = coefficient;
}
int Term::getCoefficient()
{
return coefficient;
}
void Term::setExponent(int exponent)
{
this->exponent = exponent;
}
int Term::getExponent()
{
return exponent;
}
// Overload + operator to add two Terms
Term Term::operator + (const Term& b)
{
Term term;
if(this->exponent = b.exponent) {
term.coefficient = this->coefficient + b.coefficient;
}
return term;
}
// Overload = operator to compare two terms
void Term::operator = (const Term &t ) {
coefficient = t.coefficient;
exponent = t.exponent;
}
// Overload += operator
Term& Term::operator += (const Term& rhs){
this->coefficient += rhs.coefficient;
this->exponent += rhs.exponent;
return *this;
}
// Overload -= operator
Term& Term::operator -= (const Term& rhs){
this->coefficient -= rhs.coefficient;
this->exponent -= rhs.exponent;
return *this;
}
// Overload output << operator
ostream& operator << (ostream& os, const Term& t)
{
os << t.coefficient << 'x' << '^' << t.exponent;
return os;
}
Polynomial.h
#include <vector>
class Term;
class Polynomial
{
private:
std::vector<Term*> t(5);
public:
Polynomial(std::vector<Term*> &t);
std::vector<Term*> getT();
void setT(std::vector<Term*> &t);
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.