...

View Full Version : Creating a class



debbie_lee104
08-09-2007, 01:21 AM
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.
Please help!

#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::ostream& operator << (std::ostream& out, const Polynomial&
poly);
};
#endif

oracleguy
08-09-2007, 09:56 PM
Here is how you add a friend. I didn't test the code but that is the gist of it.



#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.

altie
08-16-2007, 07:43 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum