In need of help with overloading operators for use in Polynomial Class

Mar 26, 2017 at 12:02am
I am trying to write a program that can add, subtract, multiply polynomials. It needs to work with both two polynomials and a constant and a polynomial.I need help with my arithmetic operators (+,-,*) as well as the >> and << operators. I have not yet attempted the arithmetic operators but I have given the >> a try. Below is my attempt, please help me edit my logic:
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
#include<iostream>
#include<cstdlib>
using namespace std;

typedef int* IntPtr;
class polynomial
{
public:
	polynomial(int degree_Size, int coefficient[]);
	polynomial();//sets degreeSize to 10
	polynomial(const polynomial& polynomailObject);//copy constructor
	void input(polynomial newNumber);
	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; }
private:
	int degreeSize;
	int *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)
{
	int degreeSize;
	int *privateCoefficient;
	polynomial firstPoly;
	cout << "Please enter the highest degree of your polynomial: X^";
	inputStream >> degreeSize;
	privateCoefficient = new int[degreeSize];
	for (int i = 0; i <= degreeSize; i++)
	{
		cout << "Please enter the coefficient for term X^" << i << endl;
		inputStream >> privateCoefficient[i];
	}
	firstPoly = polynomial(degreeSize, privateCoefficient);
	return inputStream;
}
/*void polynomial::input(polynomial newNumber)
{
int thisDegreeSize;
cout << "Hello. You are about to create a new polynomial." << endl;
cout << "What is the highest term in this polynomial: X^";
cin >> thisDegreeSize;
for(int i = 0;i<=thisDegreeSize;i++)
}*/
int main()
{
	polynomial number1;
	cin >> number1;
	system("pause");
	return 0;
}
Mar 26, 2017 at 5:34pm
Line 18: newNumber must be passed by reference.
Line 85: shouldn't firstPoly be newNumber? Also don't forget to delete[] privateCoefficients
Mar 26, 2017 at 9:55pm
Alright, sorry for the late reply. I had this discussion in two threads and I got a little confused haha. I had been working on this problem more and I was able to get my >> and << operators coded and I can enter in my polynomials. Now all I need to do is get them to add, subtract, and multiply. Adding and subtracting are going to be fairly similar and multiplication will involve the FOIL method but again shouldnt be too hard after learning the pattern for the + and - operators. Also after I end the program I trigger a breakpoint somehow? Not sure how that happens. I think I addressed those issues in my most recent attempt, does this look better?
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
#include<iostream>
#include<cstdlib>
using namespace std;

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()
{
	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]]));
		}
	}
}*/
int main()
{
	polynomial number1, number2, number3;
	cin >> number1;
	cin >> number2;
	cout << number2;
	number3 = number2;
	cout << number3;
	system("pause");
	return 0;
}
Mar 27, 2017 at 2:50pm
Lines 21 & 22: Shouldn't the data be private?

Line 63 allocates the new array, but you assign the coefficients directly into newnumber.privateCoefficients. You need to
- delete the old coefficients array in newNumber.
- create a new array that's the right size.

You could simplify the copy constructor by initializing the object to a simple default and then using the assignment operator:
1
2
3
4
5
polynomial::polynomial(const polynomial& polynomialObject) :
    degreeSize(0), privateCoefficient(nullptr)
{
	*this = polynomialObject;
}


You may want to add a constructor that creates a polynomial from an int:
Polynomial(int constant);
Once you have this, I *think* you can add/subtract/multiply/divide a polynomial and a constant without adding any additional code. The compiler will convert the int to a polynomial.

Topic archived. No new replies allowed.