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;
}