// polygon3.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 Square: public Polygon { public: Square(): Polygon(4) { // Call base class constructor with required args cout << "In Square constructor" << endl; } }; class Triangle: public Polygon { public: Triangle(): Polygon(3) { // Call base class constructor with required args cout << "In Triangle constructor" << endl; } }; int main() { Triangle MyTriangle; Polygon MyPentagon(5); MyTriangle.NumSides = 4; // maybe we shouldn't be allowed to do this? MyTriangle.Draw(); MyPentagon.Draw(); }