#include class Plant { public: Plant(int init_a) : a(init_a) {} virtual int leaf(int x) { return x * a; } int stem(int x) { return x * leaf(x); } private: int a; }; class Tree : public Plant { public: Tree(int init_a, int init_b) : Plant(init_a), b(init_b) {} virtual int leaf(int x) { return x * b; } int stem(int x) { return x * leaf(b); } virtual int trunk(int x) { return x * stem(x); } private: int b; }; class Moss : public Plant { public: Moss(int init_b) : Plant(0), b(init_b) {} int stem(int x) { return b; } virtual int leaf(int x) { return x * stem(x); } private: int b; }; int main() { Plant p(3); Tree t(20,50); Moss m(-4); // Illegal : no such constructor // Moss n(0,1); Plant * p_ptr = &t; Plant * q_ptr = &m; // Illegal: downcast to subclass pointer not permitted // Tree * t_ptr = p_ptr; Tree * u_ptr = &t; // Illegal: no trunk method defined on plant class. // cout << p_ptr->trunk(3) << endl; cout << u_ptr->trunk(3) << endl; // Note that Plant::stem() has a virtual call to leaf(), resulting // in a call to Tree::leaf(). cout << p_ptr->stem(4) << endl; return 0; }