Can someone help me overload operators?

Hi guys, I am supposed to create a program that can add, subtract, and multiply polynomials with constants or other polynomials. I need help overloading my arithmetic operators as well as my << and >> operators. If someone could also take a look at what I have so far and make sure that it looks good and makes sense, that would be really helpful. I'm still pretty new so I might need the vocab to be simplified haha. My original thought process is that I could use the dynamic array where the size and index number of the array will be the degrees so a[2] would be X^2 and and a[0] refers to a constant, then the actual value stored at that index place would be the coefficient of the x term. Here is what my class structure looks like, as well as the functions that I have already attempted to code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 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;
};

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
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;
}
Last edited on
"Friend" is not needed for operators.
You need to make the return value an address.

In your class definition:
 
const polynomial& operator +(const polynomial& number1, const polynomial& number2);


In you other file:
1
2
3
4
5
const polynomial& polynomial::operator +(const polynomial& number1, const polynomial& number2)
{
//make number3 = number1+number2
//return number3
}

The others are similar.
Topic archived. No new replies allowed.