### Need help pulling data from a dynamic array

I have to implement a polynomial class in which you can overload addition, subtraction, and multiplication. I believe that I have the constructors and the destructor set up correctly. I can't figure out how to pull the array from the heap in order to perform the necessary operations. Any help would be appreciated.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` ``````#include #include #include using namespace std; typedef double* doubleArrayPtr; class Polynomial { public: Polynomial(); Polynomial(double); Polynomial(doubleArrayPtr, int); Polynomial(Polynomial&); ~Polynomial(); friend const Polynomial operator +(const Polynomial& poly1, const Polynomial& poly2); friend const Polynomial operator -(const Polynomial& poly1, const Polynomial& poly2); friend const Polynomial operator *(const Polynomial& poly1, const Polynomial& poly2); private: doubleArrayPtr coefArray; int degree; }; double evaluate(double var); int main() { doubleArrayPtr coef; int d; cout << endl; cout << "What is the degree of the first polynomial? "; cin >> d; coef = new double [d + 1]; cout << "Enter the coefficients (descending order): " << endl; for (int i = d; i >= 0; i--) cin >> coef[i]; Polynomial poly1(coef, d); cout << "What is the degree of the second polynomial? "; cin >> d; coef = new double [d + 1]; cout << "Enter the coefficients (descending order): " << endl; for (int i = d; i >= 0; i--) cin >> coef[i]; Polynomial poly2(coef, d); } Polynomial::Polynomial() { degree = 0; coefArray = new double[degree + 1]; coefArray[0] = 0; } Polynomial::Polynomial(double coeff) { degree = 0; coefArray = new double[degree + 1]; coefArray[0] = coeff; } Polynomial::Polynomial(doubleArrayPtr coeff, int deg) { degree = deg; coefArray = new double[degree + 1]; for (int i = 0; i <= degree; i++) coefArray[i] = coeff[i]; } Polynomial::Polynomial(Polynomial& poly) { degree = poly.degree; coefArray = new double[degree + 1]; for (int i = 0; i <= degree; i++) coefArray[i] = poly.coefArray[i]; } Polynomial::~Polynomial() { delete [] coefArray; }``````
You have a `coefArray` member in your class which you can iterate over with a loop, which you've already demonstrated you can do. your operators are declared as friend functions which means they also have access to `coefArray` for your arguments.

 ``123`` ``````Polynomial polySum(0, deg); for (int i = 0; i < deg; ++i) polySum.coefArray[i] = poly1.coefArray[i] + poly2.coefArray[i];``````
Topic archived. No new replies allowed.