th = new centigrade(); th = new fahrenheit(); th->set(f); f = th->get();
class thermometer {\fbox{thermometer}
public: virtual void set(float v) { temp = v; } virtual float get() { return temp; } protected: float temp; thermometer( float v ) : temp(v) { } };
class centigrade : public thermometer {\c{\fbox{centigrade}}
public: centigrade() : thermometer(0) { } void set(float v) { temp = v + 273; } float get() { return temp - 273; } };
class fahrenheit : public thermometer {\c{\fbox{fahrenheit}}
public: fahrenheit() : thermometer(0) { } void set(float v) { temp = (v - 32) * 5/9 + 273; } float get() { return temp * 9/5 + 32 - 273; } };
class displayer : public window {\c{\fbox{displayer}}
public: displayer(); void put(char* s); void put(float f); };
class prompter : public window {\c{\fbox{prompter}}
public: prompter(char* text); float get(); char* gets(); };
class event {\fbox{\fbox{event}}
public: void dependent(event* e); void process(); virtual void operator()() = 0; private: set* dep; };
class update : public event {\fbox{update}
public: update(thermometer* th, prompter* p) : _th(th), _p(p) {} void operator()() { _th->set( _p->get() ); event::process(); } protected: thermometer* _th; prompter* _p; };
class show : public event {\fbox{show}
public: show(thermometer* th, displayer* d) : _th(th), -d(d) {} void operator()() { _d->put( _th->get() ); event::process(); } protected: thermometer* _th; displayer* _d; };
thermometer* c = new centigrade(); thermometer* f = new fahrenheit(); displayer* cd = new displayer("centigrade"); displayer* fd = new displayer("fahrenheit"); prompter* cp = new prompter("enter centigrade value"); prompter* fp = new prompter("enter fahrenheit value"); show* sc = new show(c,cd); show* sf = new show(f,fd); update* uc = new update(c,cp); update* uf = new update(f,fp);
uc->dependent(sc); uc->dependent(sf); uf->dependent(sc); uf->dependent(sf);
menu->insert(uc); menu->insert(uf);