In: Computer Science
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a default constructor.) Remove the existing constructor. Write one constructor that takes an array of doubles and the number of entries and uses the array to make a Polynomial object. Use the append method to do this. Overload the operators +, -, and * so you can add, subtract and multiply polynomials. Overload the output operator << so you can output a representation of your operator to cout or a file. Overload the [ ] operator so you can extract or change a coefficient of a polynomial. Write a method degree that returns the degree of the polynomial. Write a method eval that takes a double argument x and returns the value of the Polynomial at x. Be sure to write a destructor, copy constructor, and assignment operator. The Polynomial class should have private member variables coeffs_ for the array of coefficients, size_ for the number of positions used in the array, capacity_ for the capacity of the array of coefficients.
(C++)
Ans :
Program
plynomial.h file
#include<iostream>
using namespace std;
//Create a class Polynomial
class Polynomial {
//Attributes
private:
double* coefficients;
int size_used;
//Member functions
public:
//Constructor
Polynomial(double Coeffs[], int terms);
//Destructor
~Polynomial();
//Copy constructor
Polynomial(const Polynomial& a);
//Operator overloads
Polynomial& operator= (const Polynomial&
rhs);
const Polynomial operator+ (const Polynomial&
other) const;
const Polynomial operator- (const Polynomial&
other) const;
const Polynomial operator* (const Polynomial&
other) const;
friend ostream & operator << (ostream
&out, const Polynomial &c);
double operator[](int index);
//Evaluate a polynomial
double evaluateAt(int x);
};
Polynomial.cpp
#include "plynomial.h"
//Constructor allocate array with passed size and allocate
values
Polynomial::Polynomial(double Coeffs[], int N_terms) {
size_used= N_terms;
coefficients= new double[size_used];
for (int i = 0; i < size_used; i++)
coefficients[i] =
Coeffs[i];
}
//Destructor, deallocate allocated memory
Polynomial::~Polynomial() {
if (coefficients)
{
delete[] coefficients;
coefficients = nullptr;
}
}
//Copy constructor
Polynomial::Polynomial(const Polynomial& a) {
size_used = a.size_used;
coefficients = new double[size_used];
for (int i = 0; i < size_used; i++)
coefficients[i]
=a.coefficients[i];
}
//Assignment operator overload
Polynomial& Polynomial::operator= (const Polynomial& a)
{
if (this == &a)
return *this;
this->size_used = a.size_used;
coefficients = new double[size_used];
for (int i = 0; i < size_used; i++)
this->coefficients[i] =
a.coefficients[i];
return *this;
}
//Operator + overload
const Polynomial Polynomial::operator+ (const Polynomial&
other) const {
int maxSize = (size_used > other.size_used) ?
size_used : other.size_used;
double *a;
a=new double[maxSize];
for (int i = 0; i <= maxSize; i++) {
a[i] = coefficients[i] +
other.coefficients[i];
}
Polynomial p(a,maxSize);
return p;
}
//Operator - overload
const Polynomial Polynomial::operator- (const Polynomial&
other) const {
int maxSize = (size_used > other.size_used) ?
size_used : other.size_used;
double *a;
a = new double[maxSize];
for (int i = 0; i <= maxSize; i++) {
a[i] = coefficients[i] -
other.coefficients[i];
}
Polynomial p(a, maxSize);
return p;
}
//Operator * overload
const Polynomial Polynomial::operator* (const Polynomial&
other) const {
int maxSize = (size_used > other.size_used) ?
size_used : other.size_used;
double *a;
a = new double[maxSize];
for (int i = 0; i <= maxSize; i++) {
a[i] = coefficients[i] *
other.coefficients[i];
}
Polynomial p(a, maxSize);
return p;
}
//Function to evaluate polynomial value
double Polynomial::evaluateAt(int x)
{
double sum = 0.0;
double xPow = 1.0;
if (coefficients)
for (int i = 0; i <
size_used; i++)
{
sum +=
xPow * coefficients[i];
xPow
*= x;
}
return sum;
}
//Extraction operator overload
ostream& operator<< (ostream &out, const Polynomial
&c)
{
out <<c.coefficients[c.size_used-1]<< "x^"
<< c.size_used - 1;
for (int i = c.size_used - 2; i >= 0; i--)
std::cout << " + "
<< c.coefficients[i] << "x^" << i;
return out;
}
//Overload index operator
double Polynomial::operator[](int index) {
if (index >= size_used)
{
cout << "No element in
that coefficient\n";
return -1;
}
return coefficients[index];
}
test.cpp
/*
Test class
*/
#include "plynomial.h"
int main()
{
//Two double arrays for check
double coeff1[] = { 1.2,3.4 };
double coeff2[] = { 1.2,3.4,1.5 };
//Create 2 polynomials
Polynomial p1(coeff1, 2);
Polynomial p2(coeff1, 2);
//Evaluate check
cout <<"Evaluate check:
"<<p1.evaluateAt(1) << endl;
//Add check
Polynomial p3=p1 + p2;
cout <<"Addition check: "<< p3 <<
endl;
//Assignment check
p3 = p1;
cout << "Assignment check: " << p3
<< endl;
//Subtract check
Polynomial p4(coeff2, 3);
p3 = p4-p3;
cout << "Subtraction check: " << p3
<< endl;
//Index check
cout << "Index check: " << p4[1] <<
endl;
}
Output :
Evaluate check: 4.6
Addition check: 6.8x^1 + 2.4x^0
Assignment check: 3.4x^1 + 1.2x^0
Subtraction check: 1.5x^2 + 0x^1 + 0x^0
Index check: 3.4
Thank you...