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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cmath>
#include <cstdlib>

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.

1
2
3
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.