Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,561 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,895 people online right now. Registration is fast and FREE... Join Now!




Class Implementation

 
Reply to this topicStart new topic

Class Implementation, I need to implement a polynomial class but isn't working at all

ambrosia1
11 Apr, 2008 - 03:47 PM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 3

CODE
#include "Poly.h"
#include <cmath>

Poly::Poly(int order)       // Default constructor
{
    int i;
    order = order + 1;
    a = new double [order+1];
    for (i=0;i<order+1;i++) a[i] = 0.0;
}

Poly::Poly(const Poly &p)  // Copy constructor
{
    int i;
    order = p.order+1;
    a = new double [order+1];
    for (i=0;i<order+1;i++) a[i] = p.a[i];

}

Poly::~Poly()
{
delete [] a;
}

//Functions

void Poly::Resize(int newlength)
{  
    if (order+1 != newlength) {  
    if (order+1 > 0) delete [] a;
    a = new double [newlength];
    order = newlength;
  }  
}

Poly Poly:: operator=(const Poly &p)
{
  int i;
  Resize(p.order+1);
  for (i=0; i< order+1; i++) a[i] = p.a[i];  
  return(*this);  

}
Poly Poly::operator+(const Poly &p)
{
  int i;
  Poly temp(*this);
  for (i=0; i< order+1; i++) temp.a[i] += p.a[i];
  return temp;

}

Poly Poly::operator-(const Poly &p)
{
int i;
  Poly temp(*this);

  for (i=0; i< order+1; i--) temp.a[i] -= p.a[i];
  return temp;
}

Poly Poly::operator*(const Poly &p){

        int i, j;
        Poly temp;
        double b;
        for (i=0;i<order+1; i++) {
            b = a[i];
            for (j=0; j <p.order+1;j++) {
                temp = temp+ b*p.a[j];
            }
        }
        return temp;
}

double Poly::Evaluate(double a){  // Compute and return the polynomial value at x
    /*double temp, b;
    int i;
    for (i=0; i<order+1; i++) {
        b = a[i];
        temp +=  b * pow(a, i); // something wrong here!!!
    }
    return temp;*/
    return 0;
}

void Poly::SetCoeff(int o, double a){

    return;
}

istream &operator>>(istream &is, Poly &p)
{
    cout<<"Enter Polynomial order: \n";
    int order;
    is>>order;
    p.Resize(order+1);

    cout<<"Enter coefficients: \n";
    int i;
    for (i=0; i<p.order+1; i++) {
        double b;
        is>>b;
        p.a[i]=b;
    }
    return is;

}
ostream &operator<<(ostream &os, Poly &p)
{
    int i;
    for (i=p.order; i>1; i--) {
        os<<p.a[i]<<"*x^"<<i-1<<"+";
    }
    os<<p.a[1];
    return os;
}


*Mod Edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Class Implementation
11 Apr, 2008 - 03:54 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 50 times
Dream Kudos: 550
My Contributions
what is the problem?
User is online!Profile CardPM
+Quote Post

ambrosia1
RE: Class Implementation
11 Apr, 2008 - 03:58 PM
Post #3

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 3

I can't figure out how to implement the addition operator to add two different polynomials of possibly different lengths and what i have there is completely wrong

User is offlineProfile CardPM
+Quote Post

ambrosia1
RE: Class Implementation
11 Apr, 2008 - 04:48 PM
Post #4

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 3

s$#@ s)(& s%^! f*#@ f!*& f^#% me this blows

*Mod edit: smile.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Class Implementation
12 Apr, 2008 - 07:48 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 50 times
Dream Kudos: 550
My Contributions
Well your addition operator looks ok to me... you just need to add in a little logic to deal with the different lengths. I don't think the line in the constructor order = order + 1; does what you think it will. This is the same as writing order++ because you have two different scopes, and so the compiler only looks at the top scope (in this case the function scope). It is generally better not to duplicate symbol names, but if you must then you will need to make sure you tell the compiler which symbol you mean,

try: this.order = order +1;

Since you have the order set then you can look to see which has the maximum order, if it is not temp then change the order of temp to match that of a and then do the addition as normal. Tada.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month