struct expr { virtual bool eval() const = 0; }
struct True : public expr {bool eval() const {return true;}};
class Not : public expr { expr exp; public: Not( const expr & e ) : exp( &e ) {} bool eval() const {return e->eval();} };
class And : public expr { expr exp1, *exp2; public: And( const expr & e1, const expr & e2 ) : exp1( &e1 ), exp2( &e2 ) {} bool eval() const {return exp1->eval() && e2->eval();} };
And so forth. Different type constructors -> different sub-classes of the abstract type. Functions operating on the type -> methods of the parent class, that each sub-class overrides.
If instead of expr and True/False we had Shape and Triangle/Rectangle that we're more used to looking as forming a hierarchy, it might make it easier to see my point. Isn't this the Java/C++ way for doing so? If not, what am I missing here?