// ------------------------------------------------------------------------- // shape.cpp - classe para representacao de figuras geometricas planas. // ------------------------------------------------------------------------- // Criado: 31-Out-1999 Márcio Santi // // Modificado: // ------------------------------------------------------------------------- #include #include // ------------------------------------------------------------------------- // Simbolos auxiliares: // enum { CIRCLE = 1, RECTANGLE, }; typedef struct { double x,y; } Point; const double PI = 3.14159265358979323846; // ------------------------------------------------------------------------- // Definicao da classe Shape: // class Shape { protected: int type; // Tipo da figura public: Shape(void) { cout << "Shape\n"; } virtual ~Shape(void) { cout << "~Shape\n"; } int GetType(void) { return type; } virtual double GetArea(void) = 0; virtual void Read(void) = 0; virtual void Print(void) = 0; }; // ------------------------------------------------------------------------- // Definicao da classe Circle: // class Circle : public Shape { protected: Point cnt; // Centro double rad; // Raio public: Circle(void); Circle(Point, double); ~Circle(void) { cout << "~Circle\n"; } double GetArea(void){ return PI*rad*rad; } void Read(void); void Print(void); }; // ================================= Circle ================================ Circle :: Circle(void) { cout << "Circle\n"; type = CIRCLE; cnt.x = cnt.y = 0.0; rad = 1.0; } // ================================= Circle ================================ Circle :: Circle(Point c, double r) { cout << "Circle\n"; type = CIRCLE; cnt = c; rad = r; } // ================================== Read ================================= void Circle :: Read(void) { cin >> cnt.x >> cnt.y >> rad; } // ================================== Print ================================ void Circle :: Print(void) { cout << "Circle:\n"; cout << "Center = (" << cnt.x << ", " << cnt.y << ")\n"; cout << "Radius = " << rad << "\n"; } // ------------------------------------------------------------------------- // Definicao da classe Rectangle: // class Rectangle : public Shape { protected: Point p0; // Canto inferior esquerdo double wdt; // Largura double hgt; // Altura public: Rectangle(void); Rectangle(Point, double, double); ~Rectangle(void) { cout << "~Rectangle\n"; } double GetArea(void){ return wdt*hgt; } void Read(void); void Print(void); }; // ================================ Rectangle ============================== Rectangle :: Rectangle(void) { cout << "Rectangle\n"; type = RECTANGLE; p0.x = p0.y = 0.0; wdt = hgt = 1.0; } // ================================ Rectangle ============================== Rectangle :: Rectangle(Point p, double w, double h) { cout << "Rectangle\n"; type = RECTANGLE; p0 = p; wdt = w; hgt = h; } // ================================== Read ================================= void Rectangle :: Read(void) { cin >> p0.x >> p0.y >> wdt >> hgt; } // ================================== Print ================================ void Rectangle :: Print(void) { cout << "Rectangle:\n"; cout << "Point = (" << p0.x << ", " << p0.y << ")\n"; cout << "Width = " << wdt << "\n"; cout << "Height = " << hgt << "\n"; }