Please answer: a little doubt that occurs when reading C ++ Master Andrew Koenig>

xiaoxiao2021-03-06  18

Chapter 8, "An Example of Object-Oriented Examples is about the processing of mathematical expressions between , -, *, /. The procedure is as follows: (C Builder6.0 Build passes, running is wrong. The inside of the book is obviously less than the numerical error, has been changed.) #Include

class Expr; class Expr_node {friend ostream & operator << (ostream &, const Expr &); friend class Expr; int use; protected: Expr_node (): use (1) {} virtual ~ Expr_node () {} virtual void print (ostream &) Const = 0;}; // ------------------------------------------ ---------- Class Expr {Friend Ostream & Operator << (Ostream &, Const Expr &); Exp_Node * P; Public: Expr (Int); EXPR (Const String &, EXPR); EXPR (Const String &, EXPR, EXPR); ~ expr () {delete p;} public: expr (const expr & t) {p = TP; P-> use;} expr & operator = (const expr & t);}; // -------------------------------------------------- class Int_node: public Expr_node {friend class Expr; int n; Int_node (int k): n (k) {} void print (ostream & o) const {o << n;}}; class Unary_node: public Expr_node {friend class Expr String op; unary_node (const string a, expr b): op (a), opnd (b) {} void print (ostream & o) const {o << "(" << OP << Opnd << ")";}}; class binary_node: public evr_node {friend CLA SS EXPR; STRING OP; Expr Left; Expr Right; Binary_Node (Const String & A, EXPR B, EXPR C): OP (a), Left (b), Right (c) {} Void Print (Ostream & O) Const {O << "(" << Left << OP << Right << ")";}}; Ostream & Operator << (Ostream & O, Const Expr & T) {TP-> Print (O); Return O;}; expr :: EXPR (INT N) {P = new int_node (n);}; expr :: expr (const string & op, expr T) {p = new unary_node (op,}; expr :: expr (const string) OP, expr left, expr right) {p = new binary_node (op, left, right);}; expr &

转载请注明原文地址:https://www.9cbs.com/read-42335.html

New Post(0)