// polygon8.cc #include #include using namespace std; class Polygon { public: int NumSides; virtual void Draw(void) { cout << "Drawing polygon with " << NumSides << " sides." << endl; } Polygon(int sides) { NumSides = sides; } }; class Triangle: public Polygon { public: Triangle(): Polygon(3) { } void Draw(void) { // now this will get used cout << "Using special triangle drawing routine" << endl; } }; void DrawPolygon(Polygon& poly) { poly.Draw(); } int main() { Polygon MyPentagon(5); Triangle MyTriangle; DrawPolygon(MyPentagon); // Now thanks to virtual functions, the correct (more specialized) // version of the Draw() routine will be used, i.e., Triangle::Draw() DrawPolygon(MyTriangle); }