// polygon7.cc #include #include using namespace std; class Polygon { public: int NumSides; 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) { // this won't get used in this example cout << "Using special triangle drawing routine" << endl; } }; void DrawPolygon(Polygon& poly) { poly.Draw(); } int main() { Polygon MyPentagon(5); Triangle MyTriangle; DrawPolygon(MyPentagon); // Since we pass MyTriangle as a generic Polygon to DrawPolygon, // we wind up drawing with the generic Polygon::Draw() not the // specialized Triangle::Draw() like you might expect! DrawPolygon(MyTriangle); }