// polygon5.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; cout << "In Polygon constructor with " << NumSides << " sides" << endl; } }; class Triangle: public Polygon { public: Triangle(): Polygon(3) { // Call base class constructor with required args cout << "In Triangle constructor" << endl; } void Draw(void) { // This routine overrides the base class Draw() routine cout << "Using special triangle drawing routine" << endl; } }; int main() { Triangle MyTriangle; Polygon MyPentagon(5); MyTriangle.Draw(); // uses specialized Triangle::Draw() routine MyPentagon.Draw(); // uses generic Polygon::Draw() routine }