Overloading for a polynomial program

Hi there, I'm kind of stumped on my program where I'm suppose to overload an addition operator (+) to add two Polynomials.

1
2
3
4
Polynomial Polynomial::operator+(Polynomial &other)
{
  //insert code
}


I was able to store the coefficients in an array based on the how many degrees there are in one of my 'set' function. Not sure if I'm on the right track or not, any tips is appreciated.
You have to show more:
* what are you overloading?
* what are the class members (and possibly their meanings)?
Header file
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
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;

class Polynomial
{ 
public:
	Polynomial();
	~Polynomial();

	Polynomial operator+(Polynomial &other);
	friend ostream& operator << (ostream &out, const Polynomial &polynomial);

	int getCoefficient();
	int getExponent();

	void setExponent(int exp);
	void setCoefficient();

private:
	int Coefficient;
	int Exponent;
};

#endif 


Cpp file
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
#include <iostream>
#include "Polynomial.h"
using namespace std;

Polynomial::Polynomial()
{
}

Polynomial::~Polynomial()
{
}

Polynomial Polynomial::operator+(Polynomial &other) 
{
	//...
}

ostream& operator << (ostream& out, const Polynomial &poly)
{
	//...
}

int Polynomial::getCoefficient() 
{
	return Coefficient;
}

void Polynomial::setCoefficient()
{
	int coe[9]; //First array size declared
	for (int i = 1; i < getExponent()+1; i++) //Loop to ask user for coefficient base on the degree of the polynomial
	{
		cout << "Please enter your coefficient for the first polynomial " << i << endl;
		cin >> Coefficient; 
		coe[i] = Coefficient;
	}

	cout << "Your first polynomial is ";
	for (int i = 1; i < getExponent() + 1; i++)
	{
		cout << coe[i] << "x^" << i << " + ";
	}
	cout << endl;

	int coe2[9]; //Second array size declared
	for (int i = 1; i < getExponent() + 1; i++) 
	{
		cout << "Please enter your coefficient for the second polynomial " << i << endl;
		cin >> Coefficient;
		coe2[i] = Coefficient;
	}

	cout << "Your second polynomial is ";
	for (int i = 1; i < getExponent() + 1; i++)
	{
		cout << coe2[i] << "x^" << i << " + ";
	}
	cout << endl;
}

int Polynomial::getExponent()
{
	return Exponent;
}

void Polynomial::setExponent(int exp) 
{
	Exponent = exp;
}


I'm confused on how to overload the addition operator to add two polynomials that was asked from setCoefficient() and how to output it by overloading the stream insertion operator.
Would it not be easier to include most of the code in setCoefficient in your main statement or print function?
Also, as a pointer, getCoefficient should probably take an exponent as input and return a coefficient.
How dows your class handle 2x2 + 3x + 5? I am a little confused?
Topic archived. No new replies allowed.