Help with Overloading Operators for Polynomial Class?

Mar 26, 2017 at 7:26pm
Hi, I am trying to write a program that can add, subtract, and multiply polynomials with my class type polynomial. In order to do this, I need to overload the operators +,-,*,=. No need to worry about division. I thought I had a pretty good start on it but i cant get this program to work. Can someone help? Here is both my class structure and what I have done so far:

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
typedef int* IntPtr;
class polynomial
{
public:
	polynomial(int degree_Size, int coefficient[]);
	polynomial(int coefficient[]);
	polynomial();//sets degreeSize to 10
	polynomial(const polynomial& polynomailObject);//copy constructor
	polynomial& operator = (const polynomial& rightSide);
	~polynomial();
	friend const polynomial operator +(const polynomial& number1, const polynomial& number2);
	friend const polynomial operator -(const polynomial& number1, const polynomial& number2);
	friend const polynomial operator *(const polynomial& number1, const polynomial& number2);
	friend istream& operator >> (istream& inputStream, polynomial& newNumber);
	friend ostream& operator <<(ostream& outputStream, const polynomial& number);
	int getDegreeSize()const { return degreeSize; }
	int degreeSize;
	int *privateCoefficient;

};
polynomial::polynomial(int coefficent[])
{
	coefficent = privateCoefficient;
}
polynomial::polynomial(int degree_Size, int coefficient[])
{
	degreeSize = degree_Size;
	privateCoefficient = new int[degreeSize];
	for (int i = 0; i <= degreeSize; i++)
	{
		privateCoefficient[i] = coefficient[i];
	}
}
polynomial::polynomial()
{
	degreeSize = 10;
	privateCoefficient = new int[degreeSize];
}
polynomial::~polynomial()
{
	if (privateCoefficient)
	{
		delete[] privateCoefficient;
		privateCoefficient = NULL;
	}
}
polynomial::polynomial(const polynomial& polynomialObject) :degreeSize(polynomialObject.getDegreeSize())
{
	privateCoefficient = new int[degreeSize];
	for (int i = 0; i < degreeSize; i++)
	{
		privateCoefficient[i] = polynomialObject.privateCoefficient[i];
	}
}
polynomial& polynomial::operator = (const polynomial& rightSide) {
	if (this == &rightSide)
	{
		return *this;
	}
	else
	{
		degreeSize = rightSide.degreeSize;
		delete[] privateCoefficient;
		privateCoefficient = new int[degreeSize];
		for (int i = 0; i <= degreeSize; i++)
		{
			privateCoefficient[i] = rightSide.privateCoefficient[i];
		}
		return *this;
	}
}
istream& operator >> (istream& inputStream, polynomial& newNumber)
{
	cout << "Please enter the highest degree of your polynomial: X^";
	inputStream >> newNumber.degreeSize;
	IntPtr a;
	a = new int[newNumber.degreeSize];
	for (int i = 0; i <= newNumber.degreeSize; i++)
	{
		cout << "Please enter the X^" << i << " term coefficient: " << endl;
		inputStream >> newNumber.privateCoefficient[i];
	}
	return inputStream;
}
ostream& operator << (ostream& outputStream, const polynomial& newNumber)
{
	for (int i = newNumber.degreeSize; i >= 0; i--)
	{
		outputStream << newNumber.privateCoefficient[i] << "X^" << i<<" ";
	}
	return outputStream;
}
const polynomial operator +(const polynomial& number1, const polynomial& number2)
{
	int number1size, number2size;
	number1size = number1.degreeSize;
	number2size = number2.degreeSize;
	if (number1size > number2size)
	{
		IntPtr a;
		a = new int[number1.degreeSize];
		for (int i = 0; i <= number1size; i++)
		{
			return polynomial((number1.degreeSize), (a[number1.privateCoefficient[i]+number2.privateCoefficient[i]]));
		}
	}
}
Mar 26, 2017 at 7:54pm
Mar 26, 2017 at 8:08pm
I didn't know this was a double post, this is where my program currently stands and is a little different from the last version. I also was a little worried that I had posted this problem in the wrong discussion board. My bad!
Last edited on Mar 26, 2017 at 8:12pm
Topic archived. No new replies allowed.