#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial
{
public:
Polynomial();
Polynomial(int ,int []);
~Polynomial();
const Polynomial operator+(Polynomial& p) const;// addition
const Polynomial operator- (Polynomial& p ) const; // subtraction
int getExponent();
//void setExponent();
void setCoefficient();
int getCoefficient();
void printPolynomial();
private:
int exponent; // exponent
int coefficient[ 10 ]; // coefficients array
};
#endif
//end of class definition
//Polynomial.cpp
#include "Polynomial.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
Polynomial::Polynomial() //default constructor
{
exponent = 0;
coefficient[10] = 0;
}
Polynomial::Polynomial(int x,int c[]) //constructor
{
if(x>=0 && x<10){
exponent=x;
for(int i=0;i<=exponent;i++)
coefficient[i]=c[i];
for(int i=exponent+1;i<10;i++)
coefficient[i]=0;
}
else {
exponent=0;
for(int i=0;i<10;i++)
coefficient[i]=0;
}
}
Polynomial::~Polynomial() //destructor
{
delete coefficient;
}
const Polynomial Polynomial::operator+(Polynomial& p) const // + operator overload
{
int maxD=(exponent>p.getExponent())?exponent:p.getExponent();
int a[10];
for(int i=0;i<=maxD;i++)a[i]=coefficient[i]+p.getCoefficient();
Polynomial q(maxD,a);
return q;
}
const Polynomial Polynomial::operator-(Polynomial& p) const
{
int maxD;
int a[10];
maxD=(exponent<p.getExponent())?exponent:p.getExponent();
for(int i=0;i>=maxD;i--)a[i]=coefficient[i]-p.getCoefficient();
Polynomial q(maxD,a);
return q;
}
int Polynomial::getExponent()
{
return exponent;
}
int Polynomial::getCoefficient()
{
int n=0;
if( n >= 0 && n <= exponent)
return coefficient[n];
return 0;
}
void Polynomial::setCoefficient()
{
cout<<endl;
for (int i=0; i<coefficient[i]; i++)
{
cout<<"Enter coefficient for x^"<<i<<": ";
cin>>coefficient[i];
}
}
void Polynomial::printPolynomial()
{
for ( int i = 10; i >= 0; i-- )
{
if ( coefficient[i] != 0 )
{
cout << coefficient[i] << "x^" << i << " ";
}
}
}
//end of member functions definitions
//Output screen
First polynomial is:
-858993460x^9 -858993460x^8 -858993460x^7 -858993460x^6 -858993460x^5 -858993460
x^4 -858993460x^3 -858993460x^2 -858993460x^1 -858993460x^0
Second polynomial is:
-858993460x^9 -858993460x^8 -858993460x^7 -858993460x^6 -858993460x^5 -858993460
x^4 -858993460x^3 -858993460x^2 -858993460x^1 -858993460x^0
Adding the polynomials yields:
Press any key to continue . . .
What am I doing wrong? I also get a message "Debug assertion fail", what does that mean?