// // NAME // expr.h // // DESCRIPTION // This file contains the type definitions and function prototypes // exported by the Expression module. // #ifndef _expr_h_ #define _expr_h_ // // Include files. // #include <iostream.h> // // NAME // expr // // DESCRIPTION // This abstract class models an expression. // // FUNCTION MEMBERS // evaluate() // - evaluates the expression. // clone() // - makes a copy of the expression and returns a pointer // to it. // display(ostream &) // - outputs the expression to the given output stream. // class expr { public: virtual ~expr() { } virtual double evaluate() const = 0; virtual expr * clone() const = 0; virtual ostream & display(ostream &) const = 0; }; // // NAME // constant // // DESCRIPTION // This class models a constant expression. // // DATA MEMBERS // number - value of the expression. // // FUNCTION MEMBERS // constant(const double) // - constructs a constant expression from the given double. // constant(const constant &) // - copy constructor. // ~constant() // - destructor. // evaluate() // - evaluates the expression. // clone() // - makes a copy of the expression and returns a pointer // to it. // display(ostream &) // - outputs the expression to the given output stream. // class constant : public expr { public: constant(const double); constant(const constant &); virtual ~constant(); double evaluate() const; expr * clone() const; ostream & display(ostream &) const; private: double number; }; // // NAME // composite // // DESCRIPTION // This class models a composite expression. A composite // expression is an expression which consists of an operation // and two subexpressions. // // MEMBER TYPES // operation - kinds of operations in an expression. // // DATA MEMBERS // oper - operation of the expression. // op1 - pointer to the first operand. // op2 - pointer to the second operand. // // FUNCTION MEMBERS // composite(const composite &) // - copy constructor. // composite(const operation, expr *, expr *) // - constructs an expression from the given operation and // two subexpressions. // ~composite() // - destructor. // evaluate() // - evaluates the expression. // clone() // - makes a copy of the expression and returns a pointer // to it. // display(ostream &) // - outputs the expression to the given output stream. // class composite : public expr { public: enum operation { Addition, Subtraction, Multiplication, Division }; composite(const composite &); composite(const operation, expr *, expr *); virtual ~composite(); double evaluate() const; expr * clone() const; ostream & display(ostream &) const; private: operation oper; expr * op1; expr * op2; }; #endif