Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 3 of 3
Thread: Creating a class

08092007, 01:21 AM #1
 Join Date
 Jun 2007
 Posts
 21
 Thanks
 0
 Thanked 0 Times in 0 Posts
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 nonmember 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:
// NONMEMBER BINARY OPERATORs
static const Polynomial operator +(const Polynomial& PX, const
Polynomial& QX);
static const Polynomial operator (const Polynomial& PX, const
// NONMEMBER OUTPUT FUNCTIONS
std:stream& operator << (std:stream& out, const Polynomial&
poly);
};
#endif
08092007, 09:56 PM
#2
 Join Date
 Jun 2002
 Location
 USA
 Posts
 9,074
 Thanks
 1
 Thanked 328 Times in 324 Posts
Here is how you add a friend. I didn't test the code but that is the gist of it.
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 nonmember 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.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: // NONMEMBER BINARY OPERATORs static const Polynomial operator +(const Polynomial& PX, const Polynomial& QX); static const Polynomial operator (const Polynomial& PX, const // NONMEMBER OUTPUT FUNCTIONS std:stream& operator << (std:stream& out, const Polynomial& poly); }; #endif
OracleGuy
08162007, 07:43 PM
#3
 Join Date
 Aug 2007
 Posts
 13
 Thanks
 0
 Thanked 0 Times in 0 Posts
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?