We begin with the class definitions for the FLOAT_CELL_CORE which will form the parent of the FLOAT_POLY_CELL class:
#define FLOAT float
class FLOAT_CELL_CORE{
friend class FLOAT_POLY_CORE;
friend istream& operator>>(istream& in, FLOAT_CELL_CORE& A)
{A.grab(in); return(in);};
friend istream& operator>>(istream& in, FLOAT_CELL_CORE* A)
{A->grab(in); return(in);};
friend ostream& operator<<(ostream& out,const FLOAT_CELL_CORE& A)
{A.print(out); return(out);};
friend ostream& operator<<(ostream& out,const FLOAT_CELL_CORE* A)
{A->print(out); return(out);};
public:
virtual ~FLOAT_CELL_CORE(){};
void load_element(FLOAT x){element = x;};
virtual istream& grab(istream& in) = 0;
virtual ostream& print(ostream& out) const = 0;
protected:
FLOAT element;
};
Note that the print and grab methods are as usual pure virtual, so that they must be instantiated in the children. This core class places the FLOAT element in the core cell.
Next, we derive the child FLOAT_POLY_CELL as follows:
class FLOAT_POLY_CELL : public FLOAT_CELL_CORE{
friend class FLOAT_POLY;
friend class FLOATPOLYIter;
friend FLOAT_POLY_CELL operator*(FLOAT t,FLOAT_POLY_CELL& P)
{P.element *= t;};
friend FLOAT_POLY_CELL operator*(FLOAT_POLY_CELL& P,FLOAT t)
{P.element *= t;};
friend FLOAT_POLY_CELL& operator*=(FLOAT t,FLOAT_POLY_CELL& P)
{P.element *= t;};
friend FLOAT_POLY_CELL& operator*=(FLOAT_POLY_CELL& P,FLOAT t)
{P.element *= t;};
public:
FLOAT_POLY_CELL();
FLOAT_POLY_CELL(FLOAT x_in,unsigned int power_in);
~FLOAT_POLY_CELL();
void load_all(int p,FLOAT x_in,FLOAT_POLY_CELL *next_in)
{power = p; element = x_in; next = next_in;};
void load_next_pointer(FLOAT_POLY_CELL *next_in){next = next_in;};
load_power(int p){power = p;};
FLOAT_POLY_CELL* get_next(){ return(next);};
istream& grab(istream& in);
ostream& print(ostream& out) const;
protected:
unsigned int power;
FLOAT_POLY_CELL *next;
};
Now since we are building polynomials, it will be very convenient to define overloaded scalar mulitplication operators (these are all very straightforward and note we define them inline).
friend FLOAT_POLY_CELL operator*(FLOAT t,FLOAT_POLY_CELL& P)
{P.element *= t;};
friend FLOAT_POLY_CELL operator*(FLOAT_POLY_CELL& P,FLOAT t)
{P.element *= t;};
friend FLOAT_POLY_CELL& operator*=(FLOAT_POLY_CELL& P,FLOAT t)
{P.element *= t;};
In addition, we declare the usual friendships:
friend class FLOAT_POLY;
friend class FLOATPOLYIter;
Finally, the derived cells now add the unsigned int for storing the power of the polynomial term and the pointer to the next polynomial term.
protected:
unsigned int power;
FLOAT_POLY_CELL *next;