Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Creating a class

i'm trying to create this class and i'm having trouble getting it together.
1. How do i get the degree of a polynomial?
2. I'm trying to implement a friend function but I'm not sure how.
3. Not sure how to implement the non-member output function.

#ifndef Poly.h
#define Poly.h
#include <iostream> // Provides ostream

class Polynomial
{
public:
// CONSTANT
static const unsigned int CAPACITY=5;
static const unsigned int MAX_DEGREE=10;

//CONSTRUCTOR
Polynomial(int degree)
{

}

//MODIFICATION MEMBER FUNCTIONS
void Polynomial::add_to_coeff(double amount, unsigned int exponent)
{
coeff[exponent] += amount;
}
void clear() //Reset the polynomial to zero
{
float result;
result=0;
}

void degree() //Get the Polynomial degree
{

}
//CONSTANT MEMBER FUNCTION
void poly_eval(const Polynomial& poly)
{
double x_value;
cout << "Enter the x value: ";
cin >> x_value;
cout << "For the poly: ";
view(poly);
cout << "The evaluation returned is " << poly(x_value) << endl;
}
// FRIEND FUNCTION
friend ostream& operator >> (ostream& out, const Polynomial&
poly);
private:

// NON-MEMBER BINARY OPERATORs
static const Polynomial operator +(const Polynomial& PX, const
Polynomial& QX);
static const Polynomial operator -(const Polynomial& PX, const

// NON-MEMBER OUTPUT FUNCTIONS
std:stream& operator << (std:stream& out, const Polynomial&
poly);
};
#endif

• Here is how you add a friend. I didn't test the code but that is the gist of it.

Code:
```#ifndef Poly.h
#define Poly.h
#include <iostream> // Provides ostream

class Polynomial
{
friend std:stream& operator << (std:stream& out, const Polynomial&
poly);
public:
// CONSTANT
static const unsigned int CAPACITY=5;
static const unsigned int MAX_DEGREE=10;

//CONSTRUCTOR
Polynomial(int degree)
{

}

//MODIFICATION MEMBER FUNCTIONS
void Polynomial::add_to_coeff(double amount, unsigned int exponent)
{
coeff[exponent] += amount;
}
void clear() //Reset the polynomial to zero
{
float result;
result=0;
}

void degree() //Get the Polynomial degree
{

}
//CONSTANT MEMBER FUNCTION
void poly_eval(const Polynomial& poly)
{
double x_value;
cout << "Enter the x value: ";
cin >> x_value;
cout << "For the poly: ";
view(poly);
cout << "The evaluation returned is " << poly(x_value) << endl;
}
// FRIEND FUNCTION
friend ostream& operator >> (ostream& out, const Polynomial&
poly);
private:

// NON-MEMBER BINARY OPERATORs
static const Polynomial operator +(const Polynomial& PX, const
Polynomial& QX);
static const Polynomial operator -(const Polynomial& PX, const

// NON-MEMBER OUTPUT FUNCTIONS
std:stream& operator << (std:stream& out, const Polynomial&
poly);
};
#endif```
For the two operators you are overloading, those don't need to be friends, they can be member functions. The only time they have to be non-member friend functions is if they take a data type on the left hand side other than the data type (aka the class) they are in.

• The degree is the largest exponent with nonzero coefficient. Constant polynomials (0, 1, ...) have degree 0, quadratics like x^2 + 5x have degree 2. Make sense?

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•