// slice.cc See Chapter 12 of Budd #include class W { protected: int height, width; public: W(int h, int w): height(h), width(w){} virtual void oops() { cout << " W: " << height << ' ' << width << endl; } }; class TW : public W { int cursor; public: TW(int h, int w, int c): W(h, w), cursor(c){} virtual void oops() { cout << "TW: " << cursor << endl; } }; int main() { W w(25, 80); w.oops(); // W: 25 80 TW tw(40,90,1000); tw.oops(); //TW: 1000 W* pw = &w; pw->oops(); // W: 25 80 TW* pt = &tw; pt->oops(); //TW: 1000 pw = pt; pw->oops(); //TW: 1000 tests virtual w = tw; w.oops(); // W 40 90 tests slicing }