Hi, Beginner C++ I need help implementing a class that represents a polynomial i
ID: 3805095 • Letter: H
Question
Hi,
Beginner C++
I need help implementing a class that represents a polynomial in C++ language. I would prefer if the exact class noted below was used for implemenation with only those functions. NOTHING ELSE. Comments for what is going on would be great as well. Im just really stumped.
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;
}
This what I have for implementation so far:
#include "CPolynomial.h"
#include <iostream>
using namespace std;
CPolynomial::CPolynomial()
{
start = NULL;
}
CPolynomial::CPolynomial(int r, int c)
{
}
CPolynomial::CPolynomial(const CPolynomial &)
{
}
void CPolynomial::print()
{
termNode *current = start;
while (current != NULL)
{
cout << current->coef << "x^" << current->exp << endl;
current = current->next;
if (current)
cout << "+";
}
}
CPolynomial CPolynomial::operator=(const CPolynomial &)
{
}
CPolynomial CPolynomial::operator+(const CPolynomial &) const
{
}
#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 #include using namespace std; class Poly { private: int order; int *coeff; public: Poly(); Poly(int Order, int Default=1); Poly(int Order, int *Coeff); Poly(const Poly ©); ~Poly(); void set(); //ask the user for the coefficient values void set(int *Coeff, int size); //put the coefficient values in a external coeff vector int getOrder() const; //gets the order of the polynomial int* get() const; //returns pointer to coeff array Poly operator +(const Poly &rhs); Poly operator -(const Poly &rhs); Poly operator *(const int scale); Poly operator *(const Poly &rhs); bool operator ==(const Poly &rhs); const int & operator [](int access) const; int & operator [](int access); int operator ()(int X); Poly & operator =(const Poly &rhs); friend ostream & operatorRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.