// Prog: statements_template.cpp // prints the value of a variable #include #include #include // base class for all statements struct statement { virtual void execute () {}; }; // base class for all expressions template struct expression { virtual T get_value () const {} }; template struct lvalue : public expression { virtual void set_value (const T& v) {} }; // variable with initial value template struct variable: public lvalue { T val; variable (const T& v) : val (v) {} T get_value () const { return val; } void set_value (const T& v) { val = v; } }; // print the value of an expression template struct print : public statement { expression* exp; print (expression* e) : exp (e) {} // POST: the value of *var is printed to std::cout void execute () { std::cout << exp->get_value(); } }; int main() { variable s = 10; print output (&s); // execute statements output.execute(); std::cout << std::endl; return 0; }