0

Hello. I need to create a program to manipulate polynomials. It contains basic evaluation, addition, subtraction and multiplication of polynomials. Right now, I'm working on overloading the '+' operator to add two polynomials. While the function adds the two polynomials together correctly, when it goes to output through the overloaded '<<' operator, it always produces the following output:

Debug1

What could be wrong? I tested the '<<' overload within the '+' operator and it outputs the two polynomials added correctly.

Below is my code:

Polynomial header:

#include <iostream>
using namespace std;

class Polynomial
{
    friend ostream& operator<<(ostream&, const Polynomial&);
    friend double operator+ (const double constant, const Polynomial& polynomial);
    friend double operator- (const double constant, const Polynomial& polynomial);
    friend double operator*(const double constant, const Polynomial& polynomial);
private:
    double *poly;
    int size;
public:
    double getCoeff(int exponent);
    double evaluate(double x);
    const Polynomial& operator=(const Polynomial&);
    Polynomial operator+(const Polynomial& polynomial) const;
    Polynomial operator+(const double constant) const;
    Polynomial operator-(const Polynomial& polynomial) const;
    Polynomial operator-(const double constant) const;
    Polynomial operator*(const Polynomial& polynomial) const;
    Polynomial operator*(const double constant) const;
    Polynomial (int in_size, double in_poly[]);
    Polynomial();
    ~Polynomial();
};

Polynomial Implementation:

 #include "stdafx.h"
#include <iostream>
#include <cmath>
#include "Polynomial.h"
using namespace std;

double Polynomial::evaluate(double x)
{
    double result = 0;
    for (int i = 1; i < size; i++)
    {
            result += pow(x,i)*poly[i];
    }
    result += poly[0];
    return result;
}

double Polynomial::getCoeff(int exponent)
{
    return poly[exponent];
}

const Polynomial& Polynomial::operator=(const Polynomial& polynomial)
{
    if (this != &polynomial)
    {
        delete [] poly;
        size = polynomial.size;
        poly = new double[size];
        for (int i = 0; i < size; i++)
            poly[i] = polynomial.poly[i];
    }

    return *this;
}

Polynomial Polynomial::operator+(const Polynomial& polynomial) const
{
    Polynomial tempPoly;
    if (polynomial.size > size)
    {
        tempPoly.poly = new double[polynomial.size];
        tempPoly.size = polynomial.size;
    }
    else
    {
        tempPoly.poly = new double[size];
        tempPoly.size = size;
    }
    for (int i = 0; i < tempPoly.size; i++)
    {
        tempPoly.poly[i] = poly[i] + polynomial.poly[i];
    }
    return tempPoly;
}

ostream& operator<<(ostream& out, const Polynomial& p)
{
    for (int i = p.size - 1; i >= 0; i--)
        if (i ==0 && p.poly[i] < 0)
            out<<" - "<<abs(p.poly[i])<<"x";
        else if (i == 0 && p.poly[i] >= 0)
            out<<" + "<<p.poly[i]<<"x";
        else if(i < p.size-1 && p.poly[i] < 0)
            out<<" - "<<abs(p.poly[i])<<"x^"<<i;
        else if(i < p.size-1 && p.poly[i] >= 0)
            out<<" + "<<p.poly[i]<<"x^"<<i;
        else
            out<<p.poly[i]<<"x^"<<i;
    out<<'\n';
    return out;
}

Polynomial::Polynomial (int in_size, double in_poly[])
{
    size = in_size;
    poly = new double [size];
    for (int i = 0; i < size; i++)
    {
        poly[i] = in_poly[i];
    }
}

Polynomial::~Polynomial()
{
    delete [] poly;
}

Polynomial::Polynomial()
{
    poly = new double [3];
    size = 3;
    for (int i = 0; i < size; i++)
    {
        poly[i] = 1;
    }
}

And the driver:

// Final Project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Polynomial.h"
#include <iostream>
using namespace std;

typedef double Array[];
int _tmain(int argc, _TCHAR* argv[])
{
    Polynomial p;
    Polynomial q;
    cout<<p+q;
    cin.get();
    return 0;
}

Edited by greatman05

1
Contributor
1
Reply
8
Views
4 Years
Discussion Span
Last Post by greatman05
0

I figured out the problem; I forgot to include the copy constructor.

Here are the extra changes:

Polynomial.h

    Polynomial(const Polynomial& polynomial);

Polynomial.cpp

    Polynomial::Polynomial(const Polynomial& polynomial)
    {
        size = polynomial.size;
        poly = new double [size];
        for (int i = 0; i < size; i++)
            poly[i] = polynomial.poly[i];
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.