# Discussion of the Polynomial Class

Tom Kelliher, CS18

May 6, 1996

• Course Evaluation.
• Quiz.

# The Polynomial Class

Polynomial Class Declaration:

```#ifndef __POLYNOMIAL_H
#define __POLYNOMIAL_H

#include "list.h"

class Polynomial
{
private:
List L;
int degree;

public:
Polynomial(void) : degree(-1) {}
~Polynomial() {}
// Ensure that L's copy constructor is called.
Polynomial(const Polynomial& b) : L(b.L) {}
const Polynomial& operator=(const Polynomial& b) { L = b.L; }

int Degree(void);
int SetCoefficient(int exp, double co);
int RetrieveCoefficient(int exp, double& co);
void Print(void);
const Polynomial operator+(const Polynomial& b) const;
const Polynomial operator-(const Polynomial& b) const;
const Polynomial operator*(const Polynomial& b) const;
};

#endif
```

## Class Data Members

• L --- The list holding coefficient, exponent pairs. Manipulated using public member functions of underlying list class.
• degree --- The degree of the polynomial. Why initialize to -1? When might it be updated?

## Class Member Functions

Might be useful to define some private member functions.

Recall the list member functions:

```   List(void) : l(NULL) {}
~List();
List(const List& b);
const List& operator=(const List& b);
int Insert(int exp, double co);
int Retrieve(int exp, double& co);
int Delete(int exp);
void Print(void);   // For testing.
```

• Constructor, destructor, copy constructor, and assignment operator are taken care of.
• Degree() --- Obvious.
• SetCoefficient() --- Return 1 if successful, otherwise return 0. Consider this situation:
```Polynomial P;
P.SetCoefficient(2, 1.33);
P.SetCoefficient(2, 4.0);
```
What list operations need to be performed?
• RetrieveCoefficient() --- Return 1 if Coefficient found, otherwise 0. Actual coefficient returned through co reference parameter.
• Print() --- Pretty print the polynomial:
```2.3X^3 + -6.3X + 7.1
```
• Addition --- Two ways of calling the function.

Need a local polynomial.

What should it return?

How the function should look:

```const Polynomial Polynomial::operator+(const Polynomial& b) const
{
Polynomial Sum;

// Code for addition, using operations like:
//    degree = (Degree() > b.Degree()) ? Degree() : b.Degree();
//    check1 = Retrieve(i, co1);
//    check2 = b.Retrieve(i, co2);

return Sum;
}
```
• Subtraction, Multiplication --- Just like addition, only different.

How do we multiply?

Thomas P. Kelliher
Sun May 5 16:48:13 EDT 1996
Tom Kelliher