第八章“一个面向对象的范例”
范例是关于 +,-,*,/等数学表达式的处理。
程序如下:(C++Builder6.0 build 通过,运行不对。书内程序用明显少数字符错误,已改。)
#include
class Expr;
class Expr_node
{
friend ostream& operator 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 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, t );
};
Expr::Expr( const string& op, Expr left, Expr right )
{
p = new Binary_node( op, left, right );
};
Expr& Expr::operator = ( const Expr& rhs )
{
rhs.p - use++;
if ( --p-use == 0 ) delete p;
p = rhs.p;
return *this;
};
//--------------------------------------------------
void main()
{
// Expr t = Expr( "*", Expr( "-", 5 ), Expr( "+", 3, 4 ));
// Expr t0 = Expr( 123 ); cout