The analysis of object-oriented problem of arithmetic expression tree in the eighth chapter of C + + meditation

Source: Internet
Author: User

At first, I thought it was too dull. In the eighth chapter, the author's analysis of the object-oriented problem, I follow the design and development of the book, understand some of the concepts previously just seen in the book.
Make some notes for yourself:
1. The virtual base class is used to express all the common characteristics of the inherited classes, in this case, all of the inherited classes have output and evaluation, so we define these two functions as virtual functions.
2. The virtual base class must contain at least one pure virtual function. The pure virtual function can be defined or undefined.
3. We want to make sure that the object of a class derived from the virtual base class can be properly deconstructed, so the destructor of the virtual base class is defined as a virtual function.
4. For virtual functions, if they are not defined, you should also use a pair of {} to indicate.
5.all member functions of a friend class are friend functions of another class that can access hidden information in another class, including private members and protected members, and the friend class should be preceded by a class that uses a friend class.
6. When you want a class to be able to access private members of another class, you can declare the class as a friend of another class.
7. A friend relationship cannot be inherited, can not be passed, and is one-way. The 8.Expr constructor uses expr so that it can be nested to construct the expr object.

#ifndef expr_h#define expr_h#include<iostream> #include <string>using namespace Std;class expr_node//Abstract base class {Friend class Expr;int Use;friend ostream & operator << (ostream &, const Expr_node &);p Rotected:expr_no De (): Use (1) {}virtual int eval () const=0;virtual ~expr_node () {};p ublic:virtual void print (Ostream &) const = 0;}; Class expr//specific classes {friend class Expr_node;friend ostream& operator << (ostream & O, const Expr &); Expr_node * P;PUBLIC:EXPR (int); Expr (const string & op, expr); Expr (const string &op, expr, expr); Expr (const string &op, expr, expr,expr); Expr (const expr &t) {p = T.P; ++p->use;} expr& operator= (const Expr &); ~expr () {if (--p->use = = 0) Delete p;} int eval () const {return p->eval ();}}; Class Int_node:p ublic expr_node{friend class Expr;int N;int_node (Int k): N (k) {}void print (Ostream & O) const{o <&l T N }int eval () const{return n;}; Class Unary_node:p ublic expr_node{friend class expr;stringOp Expr OPND;  Unary_node (const string & A, Expr B): OP (a), OPND (b) {}int eval () const;void print (Ostream & O) const{o << "(" << op << opnd << ")";}}; Class Binary_node:p ublic expr_node{friend class expr;string op; Expr left, right; Binary_node (const string & A, expr B, expr C): OP (a), left (b), right (c) {}int eval () const;void print (Ostream & O) C onst{o << "(" << left << op << right << ")";}}; Class Ternary_node:p ublic expr_node{friend class expr;string op; Expr left; Expr Middle; Expr right; Ternary_node (const string & S, expr L, expr m, expr R): OP (s), left (L), Middle (m), right (R) {}void print (Ostream & O ) Const;int eval () const;}; int Binary_node::eval () const{int L = left.eval (), int r = Right.eval (), if (op = = "+") return L + r;if (op = = "-") return L- R;if (OP = = "*") return L * r;if (OP = = "/" &&r!=0) return L/r;throw "error, Bad op" + op + "int Binarynode";} int Unary_node::eval () const{if (op = = "-") return-opnD.eval (); throw "error, Bad op" + op + "int Unarynode";} expr::expr (int t) {p = new Int_node (t);} expr::expr (const string & op, Expr e) {p = new Unary_node (OP, e);} expr::expr (const string &op, expr D, expr e) {p = new Binary_node (OP, D, e);} expr::expr (const string &op, expr A, expr B, expr c) {p = new Ternary_node (OP, a, B, c);} expr& expr::operator= (const Expr &r) {r.p->use++;if (--p->use = = 0) Delete p;p = R.p;return *this;} Ostream & operator << (ostream &o, const Expr_node &t) {t.print (o); return o;} ostream& operator << (ostream & O, const Expr &r) {r.p->print (o); return o;} void Ternary_node::p rint (Ostream & O) const{o << "(" << left << "?" << Middle << ":" << Right << ")";} int Ternary_node::eval () const{if (Left.eval ()) return Middle.eval (); Elsereturn right.eval ();} #endif

#include <iostream> #include "EXPR.h" using namespace Std;int main () {Expr t = expr ("*", Expr ("-", 5), expr ("+", 3, 4)); cout << t << "=" <<t.eval () << endl;//t = Expr ("*", T, T),//cout << t << "=" << t.e Val () << Endl; Expr m = expr ("?", -1, 99,26); cout << m << "=" << m.eval () << Endl; return 0;}

The analysis of object-oriented problem of arithmetic expression tree in the eighth chapter of C + + meditation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.