Hey there, What I'm trying to do is make a function that will add two polynomials, and I don't know how to exactly fix it. My first question would be if the Polynomial operator+ function is right, or whether it needs some fixing. Any hints or advice would be appreciated. This is what I have so far:
/*-- Polynomial.h ---------------------------------------------------------
Defines the monomial and polynomial classes. Polynomial is implemented
basically as a list of monomials.
---------------------------------------------------------------------------*/
#include <iostream>
#ifndef PNOM
#define PNOM
constint CAPACITY = 1024;
//typedef int ElementType;
class Monomial {
private:
float coef;
int exp;
public:
Monomial(){};
/*----------------------------------------------------------------------
Construct a Monomial object.
-----------------------------------------------------------------------*/
Monomial(float c,int p) { coef = c; exp = p;};
/*----------------------------------------------------------------------
Construct a Monomial object with specified coeffient and exponent.
-----------------------------------------------------------------------*/
friend Monomial operator+ (Monomial &, Monomial &);
/*----------------------------------------------------------------------
Overloading the operator+ so we can sum two monomials.
-----------------------------------------------------------------------*/
friend ostream & operator<< (ostream & out, const Monomial & mono);
/*----------------------------------------------------------------------
Overloading the OUTPUT operator for Monomials.
-----------------------------------------------------------------------*/
};
typedef Monomial ElementType;
class Polynomial
{
public:
Polynomial();
/*----------------------------------------------------------------------
Construct a List object.
-----------------------------------------------------------------------*/
/***** empty operation *****/
bool empty() const;
/*----------------------------------------------------------------------
Check if a list is empty.
-----------------------------------------------------------------------*/
/***** insert and erase *****/
void insert(ElementType item, int pos);
/*----------------------------------------------------------------------
Insert a value into the list at a given position.
-----------------------------------------------------------------------*/
void erase(int pos);
/*----------------------------------------------------------------------
Remove a value from the list at a given position.
----------------------------------------------------------------------*/
/***** output *****/
void display(ostream & out) const;
/*----------------------------------------------------------------------
Display a list.
Precondition: The ostream out is open.
Postcondition: The list represented by this List object has been
inserted into out.
-----------------------------------------------------------------------*/
friend Polynomial operator+(Polynomial &, Polynomial &);
Polynomial SubstractPoly(Polynomial &);
Polynomial MultiplyPoly(Polynomial &);
private:
int mySize; // current size of list stored in myArray
ElementType myArray[CAPACITY]; // array to store the Monomials
}; //--- end of List class
//------ Prototype of output operator
ostream & operator<< (ostream & out, const Polynomial & p);
#endif
To save "some" cost of creating and constructing un-necessary Monomial objects on the stack space, you can use below style. This is documented in Scott Meyers Effective C++ and in some cases it create "less" objects.
1 2 3
Monomial operator+ (Monomial& a, Monomial& b) {
return Monomial(a.coef + b.coef, a.exp);
}
Edit: I don't know how Monomial arithmetic works so I am just commenting on the C++ language features aspect instead.