The goal of this lab is to practice dynamic memory with a simple class.
This Polynomial class will store the coefficients of a one variable polynomial in a dynamically created array. This class has two data members:
size_t _degree;
float* _coefficients;
It is straightforward to see what they represent. For example: would be:
_degree = 4
_coefficients => {2.23, 0.5, -2.0, 0.0, 7}
Notice that the array size is _degree + 1
.
You will be provided with the code of the constructors, the ToString, Minus, Assignment Operator, Equals, Read and Write methods the rest of the methods you will implement yourself. Additionally you will be provided with the unit tests to test your code.
- Your code must compile.
- Your grade will be almost the percentage of your passed unit tests.
- Your code needs to show good programming practices: indentation, meaningful variable names, identifiers convention (CamelCase for functions, camelCase for variables, _camelCase for data members, SNAKE_CASE for constants), header comments, correct file names, etc. Failure to code appropriate will result in strong points penalization.
- You will need to provide implementation of the following methods:
- Destructor
- Add
- Subtract
- Multiply
- Derive
- Evaluate
- The following methods will count toward extra credit, some code will be provided to guarantee compilation:
- Divide
- Integrate
Polynomial(size_t degree)
. GIVEN. Creates a polynomial of degreedegree
, dynamically allocates an array of sizedegree + 1
and sets the coefficients to 0.0Polynomial(size_t degree, const float* coefficients)
. GIVEN. Creates a polynomial of degreedegree
and with the values of the coefficients in thecoefficients
arrayPolynomial(const Polynomial& other)
. GIVEN. Copy constructor, creates a deep copy of theother
polynomial.~Polynomial
. TODO. Destructor, deallocates the allocated memory.const Polynomial Sum(const Polynomial& rhs)const
. TODO. Returns a polynomial that will contain the sum ofthis
withrhs
. Notice that this operation does not changethis
.const Polynomial Subtract(const Polynomial& rhs)const
. TODO. Returns a polynomial that will contain the subtraction ofthis
withrhs
. Notice that this operation does not changethis
.const Polynomial Minus()const
. TODO. Returns a polynomial that will contain the additive inverse ofthis
. For instance if the polynomial is p = 2x^2 - 2, then p.Minus() would return -2x^2 + 2. Notice that this operation does not changethis
.const Polynomial Multiply(const Polynomial& rhs)const
. TODO. Returns a polynomial that will contain the multiplication ofthis
withrhs
. Notice that this operation does not changethis
.const Polynomial Divide(const Polynomial& rhs)const
. TODO. EXTRA CREDIT. Returns a polynomial that will contain the division ofthis
withrhs
. Notice that this operation does not changethis
. The reminder is lost if there was one.const Polynomial Derive()const
. TODO. Returns a polynomial that will contain the derivative ofthis
. Notice that this operation does not changethis
.float Evaluate(float x)const
. TODO. Returns the value of evaluating the polynomial with valuex
. Notice that this operation does not changethis
.float Integrate(float a, float b)const
. TODO. EXTRA CREDIT. Returns the value of the definite integral evaluated froma
tob
. Notice that this operation does not changethis
.const Polynomial& operator=(const Polynomial& rhs)
. GIVEN. Assigns the polynomialrhs
tothis
.string ToString()const
. GIVEN. Returns astring
representation of the polynomial.bool Equals(const Polynomial& rhs)const
. GIVEN. Returnstrue
ifthis
is equal torhs
.ostream& Write(ostream& output)const
. GIVEN. Writes a polynomial from theoutput
stream.istream& Read(istream& input)
. GIVEN. Reads a polynomial from theinput
stream.