<<c++ Meditation >> Chinese People's posts and telecommunications errata
This Chinese version has a variety of pit dad's small mistakes. For example, the case of variable names, the same variable, the case is not consistent, and all the questions.
And then today I feel like I'm having a grammar problem. Questions about inheriting permissions.
In the eighth chapter of the book, the demo is about class Expr_node.
The protected keyword is used. But here the Expr_node is the base class, and the inheritance will be problematic.
The specific code is as follows:
Class expr_node{ friend ostream& operator << (ostream&, const expr_node&); Friend class Expr; int use;//@use is a counter to avoid copying objects. Protected: Public : expr_node (): Use (1) {} virtual void print (ostream&) const = 0; Virtual ~expr_node () {} virtual int eval () const = 0;};
It will be error if you use protected here.
Protected is intended to identify areas where inheritance does not occur. Here the heap of virtual functions is to inherit. Just to occupy the pit. Let the subclass be realized.
You should use public instead of protected.
Because it is possible to "challenge authority", so the problem is thrown, hoping that the willing to discuss together.
The following is the complete code. Available for testing
/* programmer:eof date:2015.05.19 file:8.4.cpp e-mail: [email protecte d] */#include <iostream> #include <string>using namespace std;/* This @Expr_node is the Base-class. */class expr_node{friend ostream& operator << (ostream&, const expr_node&); Friend class Expr; int use;//@use is a counter to avoid copying objects. Protected://public:expr_node (): Use (1) {} virtual void print (ostream&) const = 0; Virtual ~expr_node () {} virtual int eval () const = 0;}; Class expr{friend ostream& operator<< (ostream&, const expr&); Expr_node* p; Public:expr ():p (NULL) {} Expr (int); Expr (const string&, expr); Expr (const string&, 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 ();}};o stream&operator<< (ostream& o, const expr_node& e) {e.print (o); return o;} expr&expr::operator= (const expr& RHS) {rhs.p->use++; if (--p->use = = 0) {delete p; } p = RHS.P; return *this;} ostream&operator<< (ostream& o, const expr& t) {t.p->print (o); return o;} Class Int_node:public expr_node{friend class Expr; int n; Int_node (int k): N (k) {} void print (ostream& o) const {o << N;} int eval () const {return n;}}; Class Unary_node:public expr_node{friend class Expr; String op; Expr OPND; Unary_node (const string& A, Expr B): OP (a), OPND (b) {} Void print (ostream& o) const {o <& Lt "(" << op << opnd << ")"; } int eval () const {if (OP = = "-") {return-opnd.eval (); } throw "error, Bad op" + op + "int Unarynode"; }};class Binary_node:public expr_node{friend class 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 << ")"; } int eval () const {int OP1 = Left.eval (); int op2 = Right.eval (); if (op = = "-") return OP1-OP2; if (op = = "+") return OP1 + op2; if (OP = = "*") return OP1 * OP2; if (op = = "/") return OP1/OP2; if (op = = "/" && OP2! = 0) return op1/op2; Throw "error, Bad op" + op + "int Binarynode"; }}; expr::expr (int n) {p = new Int_node (n);} expr::expr (const string& OP, Expr t) {p = new Unary_node (OP, t);} expr::expr (const string& OP, expr left, expr right) {p = new Binary_node (OP, left, right);} int main () {Expr t = expr ("*", Expr ("-", 5), expr ("+", 3, 4)); cout << t << "=" << t.eval () << Endl; T= Expr ("*", T, T); cout << t << "=" << t.eval () << Endl; return 0;}
<<c++ Meditation >> Chinese People's posts and telecommunications errata