I've been having problems with my homework, regarding making a polynomial class and overloading some operators to work according to the class. I've done most of the thing, but I seem to be having a problem with my destructor, or my functions.
The problem is, according to my debugging(if I did it right, which I think I did), the returning value of my + operator function get's destructed twice, when used with my copy constructor, in something like this :
//polynomials p1 and p2 are declared and given values beforehand
Polynomial p5=Polynomial();
p5=p1+p2;
This results in a heap corruption error.
Here's my header code:
#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_
#include <iostream>
using namespace std;
class Polynomial
{
public:
Polynomial();
Polynomial(int);
~Polynomial();
Polynomial(const Polynomial &);
int getOrder() const;
double getCoefficient(int) const;
void setOrder(int);
void setCoefficient(int,double);
const Polynomial &operator=(const Polynomial &);
const bool &operator==(const Polynomial &);
const double &operator()(double &);
const bool &operator!=(const Polynomial &);
friend Polynomial operator+(const Polynomial &poly1, const Polynomial &poly2);
friend Polynomial &operator+=(Polynomial &poly1,const Polynomial &poly2);
friend Polynomial operator-(const Polynomial &poly1, const Polynomial &poly2);
friend Polynomial &operator-=( Polynomial &poly1,const Polynomial &poly2);
friend Polynomial operator*(Polynomial poly1,double num);
private:
int order;
double *coefficient;
};
#endif
and here's my overloaded + function, it's not pretty, but my problem is not the calculations, it's the memory. I declared it as a friend function in the class, and by my homework rules, i need to implement it in the main.cpp file as a free function, and not as a member function.
Polynomial operator+(const Polynomial &poly1, const Polynomial &poly2) //the overloaded +operator. makes the order of the result the bigger order and adds the coefficients for all the orders. returns the result.
{
Polynomial result;
if(poly1.order >= poly2.order)
{
result.setOrder(poly1.order);
for(int i=poly1.order;i>poly2.order;i--)
{
result.setCoefficient(poly1.order-i, poly1.coefficient[poly1.order-i]);
}
for (int i =poly2.getOrder(); i>=0;i--)
{
result.setCoefficient(poly1.order-i,poly1.coefficient[poly1.order-i]+poly2.coefficient[poly2.order-i]);
}
}
else
{
result.setOrder(poly2.order);
for(int i=poly2.order;i>poly1.order;i--)
{
result.setCoefficient(poly2.order-i, poly2.coefficient[poly2.order-i]);
}
for (int i =poly1.order; i>=0;i--)
{
result.setCoefficient(poly2.order-i,poly1.coefficient[poly1.order-i]+poly2.coefficient[poly2.order-i]);
}
}
return result;
}
We were also required to overload the = operator, and here's that function if it's needed.
const Polynomial &Polynomial::operator=(const Polynomial &poly)
{
if(this!=&poly)
{
if(order==poly.order)
{
for(int i=0;i<=order;i++)
{
coefficient[i]=poly.coefficient[i];
}
}
else
{
coefficient=new double[poly.order];
order=poly.order;
for(int i=0;i<=order;i++)
{
coefficient[i]=poly.coefficient[i];
}
}
}
return *this;
}
Please keep in mind that I'm really a beginner in coding and c++, and I appreciate any help you give.
EDIT: Adding the deep copy constructor.
Polynomial::Polynomial(const Polynomial ©) //deep copy constructor
{
order=copy.order;
coefficient=new double[copy.order];
for (int i=0;i<=order;i++)
{
coefficient[i]=copy.coefficient[i];
}
}