Hi, I need help implementing a class that represents a polynomial in C++ languag
ID: 3803968 • Letter: H
Question
Hi,
I need help implementing a class that represents a polynomial in C++ language
Must have:
3 constructors
A default constructor (no parameters)
A constructor that takes two parameters: exponent and coefficient
A Copy Constructor
A destructor
A print function
Operators =,+,*
Specification file:
#ifndef CPOLYNOMIAL_H #define CPOLYNOMIAL_H
struct termNode
{
int exp; // exponent
int coef; // coefficient
termNode * next;
};
class CPolynomial
{
public:
CPolynomial (); // default constructor
CPolynomial (int r, int c);
// constructor makes a 1 node polynomial
CPolynomial (const CPolynomial & ); // copy constructor
~ CPolynomial (); // destructor
void print(); // prints out the polynomial in descending order
CPolynomial& operator=(const CPolynomial &); // equals
CPolynomial& operator+ (const CPolynomial & ) const; // returns sum of the parameter + self
CPolynomial& operator* (const CPolynomial & ) const;
private:
termNode *polyPtr;
};
#endif
This woudl be the sample main fucntion:
int main()
{
CPolynomial poly1; //creates a null polynomial
CPolynomial poly2(2, 3); //creates 2x^3 polynomial
CPolynomial poly3(3, 4); //creates 3x^4 polynomial
poly1 = poly2 + poly3; //makes poly1 = 3x^4 + 2x^3
poly1.print(); //prints out 3x^4 + 2x^3
poly3 = poly2 * poly1; //sets poly3 to 6x^7 + 4x^6
return 0;
}
Any help would be helpful. Thank you
#ifndef CPOLYNOMIAL_H #define CPOLYNOMIAL_H
struct termNode
{
int exp; // exponent
int coef; // coefficient
termNode * next;
};
class CPolynomial
{
public:
CPolynomial (); // default constructor
CPolynomial (int r, int c);
// constructor makes a 1 node polynomial
CPolynomial (const CPolynomial & ); // copy constructor
~ CPolynomial (); // destructor
void print(); // prints out the polynomial in descending order
CPolynomial& operator=(const CPolynomial &); // equals
CPolynomial& operator+ (const CPolynomial & ) const; // returns sum of the parameter + self
CPolynomial& operator* (const CPolynomial & ) const;
private:
termNode *polyPtr;
};
#endif
This woudl be the sample main fucntion:
int main()
{
CPolynomial poly1; //creates a null polynomial
CPolynomial poly2(2, 3); //creates 2x^3 polynomial
CPolynomial poly3(3, 4); //creates 3x^4 polynomial
poly1 = poly2 + poly3; //makes poly1 = 3x^4 + 2x^3
poly1.print(); //prints out 3x^4 + 2x^3
poly3 = poly2 * poly1; //sets poly3 to 6x^7 + 4x^6
return 0;
}
Explanation / Answer
#include using namespace std; class Polynomial { private: int Nterms; double* pCoeffs;// from lowest to highest order public: // functions double evaluateAt(double x); void print(void); // constructor Polynomial( double Coeffs[], int N_terms );// full construction from given array of coefficients // destructor ~Polynomial();// destructor VERY important this case }; // full constructor. Must be passed an array of coeffs. and the array size. Polynomial::Polynomial( double Coeffs[], int N_terms ) { Nterms = N_terms; pCoeffs = new double[ Nterms ];// allocate an array to hold the coefficient values for(int i=0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.