Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    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 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:stream& operator << (std:stream& out, const Polynomial&
    poly);
    };
    #endif

  • #2
    Rockstar Coder
    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.

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

  • #3
    New Coder
    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?


  •  

    Posting Permissions

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