polynomial polynomial :: derivative(void)
{
polynomial outpoly;
outpoly._coef = new double [_degree];
outpoly._degree = (_degree-1);
for(int i=0; i<(_degree); i++)
{
outpoly._coef[i]=(i+1)*_coef[i+1];
}
return outpoly;
}
//header:
#pragma once
#include <complex>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class polynomial
{
private:
double * _coef;
int _degree;
public:
// constructors
polynomial(void);
polynomial(int degree, double * coef);
~polynomial(void);
// at: evaluates the polynomial at x or z
// returns the real/complex value of the polynomial at x/z
double at(double x) const;
complex<double> at ( complex<double> z) const;
polynomial derivative(void) const;
bool isEmpty(void);
string tostring(void);
polynomial& operator = (const polynomial& rhs);
friend ostream& operator << (ostream& os, polynomial& m);
};
//How I use it in main:
double * coef;
int degree;
cout<< "INPUT degree: ";
cin>>degree;
coef = new double [degree+1];
for(int n = 0; n<(degree+1); n++)
{
cout<< "INPUT " << n << "th degree: ";
cin>> coef[n];
cout<<endl;
}
polynomial poly(degree, coef);
cout<<"INPUT converted to: " << poly<<endl;
cout<<"EVALUATE for x = ";
double x;
cin >> x;
cout << poly << " = " << poly.at(x) << endl;
cout << "DERIVATIVE of fxn is: " << poly.derivative() <<endl;
I have code for handling polynomials via an array of coefficients. Now I want to create a function that returns its derivative in polynomial form.
Unfortunately, the return value is deleted before it is passed on. I discovered this the hardway by following the debugger as closely as I could.
How can I avoid this error?