Operation on using Class

Hello, we were tasked to create a polynomial class that will do the basic operation.

I have a working constructor already. I don't know how to make the operators, +,-,* and /. I'm new in c++ and I'm really having a hard time.
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
class Polynomial
{
    public:
        Polynomial ();
        Polynomial (double Coeff [], int inputDegree);//constructor
        Polynomial (Polynomial&);// copy constructor
        int setDegree(int deg);
        void output(void);
	   ~Polynomial();     
        
        Polynomial& operator = (Polynomial&); 
		Polynomial operator +(Polynomial& Operand);
		Polynomial operator -(Polynomial& Operand);
		Polynomial operator *(Polynomial& Operand);
		Polynomial operator /(Polynomial& Operand);

		friend ostream& operator <<(ostream &, Polynomial &);
		
	private:
        double *pointerCoeff;// array where coefficients are stored
        int Degree;// the degree of the polynomial is one less than the size of the array of coefficients
};

Polynomial::Polynomial() //This is the default constructor
{
	Degree = 0;
	pointerCoeff = new double[Degree + 1];
	pointerCoeff[0] = 0;
}
Polynomial::Polynomial( double Coeff[], int inputDegree)// this is the main contstructor
{
	Degree =inputDegree;
	pointerCoeff= new double[ Degree +1 ];// allocate an array to hold the coefficient values
	for(int i=0; i<(Degree + 1); i++)
		pointerCoeff[i] = Coeff[i];
}
Polynomial:: Polynomial (Polynomial& copy)//This is the copy constructor
{
    Degree=copy.Degree;
    copy.pointerCoeff = new double [copy.Degree + 1];
    for(int i=0; i<(Degree+1); i++)
		copy.pointerCoeff[i];
}
You've already got the header for it, you just need the implementation:

1
2
3
4
5
6
7
8
9
Polynomial& Polynomial::operator= (Polynomial& P)
{
    Degree = P.Degree;
    delete[] pointerCoeff;
    pointerCoeff = new double[Degree + 1];
    for (int i = 0; i <= Degree; ++i)
        pointerCoeff[i] = P.pointerCoeff[i];
    return *this;
}
Last edited on
THANK YOU. But I'm still a bit confused on how to make the operator +.

1
2
3
4
5
6
7
8
9
10
11
Polynomial Polynomial::operator + (Polynomial& operand)
{
    Polynomial sum;
    for (int d= this->Degree ; d>=0;d--)
    {
        if (sum.Degree=operand.Degree)
        sum.pointerCoeff[d]= this->pointerCoeff[d]+operand.pointerCoeff[d];
        sum.Degree=this->Degree;
    }
    return sum;
}
Last edited on
I see a beginner mistake.
if (sum.Degree=operand.Degree)
should be:
if (sum.Degree==operand.Degree)

The only other problem I see is that if the two polynomials have different degrees, nothing will happen.

I changed it to this plus I also added the const in the operator and in the header. It still has an error.

Sorry for the trouble. I am a beginner
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Polynomial Polynomial::operator + (const Polynomial& operand)
{
    Polynomial sum;
    for (int d= this->Degree ; d>=0;d--)
    {
        if (this->Degree==operand.Degree)
        {
            sum.pointerCoeff[d]= this->pointerCoeff[d]+operand.pointerCoeff[d];
            sum.Degree=this->Degree;
        }
        
        else if (this->Degree!=operand.Degree)
        {
            sum.pointerCoeff[d]= this->pointerCoeff[d];
            sum.Degree=this->Degree;
        }   
    }
    return sum;
}
Topic archived. No new replies allowed.