























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























class Shape {
ā¦
protected:
char _type;
public:
Shape() { } void draw(){ cout << āShape\nā; } int calcArea() { return 0; } char getType() { return _type; }
}
class Line : public Shape {
ā¦
public:
Line(Point p1, Point p2) {
⦠} void draw(){ cout << āLine\nā; }
}
class Triangle : public Shape {
ā¦
public:
Triangle(Line l1, Line l2, double angle) { ⦠} void draw(){ cout << āTriangle\nā; } int calcArea() { ⦠}
}
int main() {
Shape* _shape[ 10 ]; Point p1(0, 0), p2(10, 10); shape[1] = new Line(p1, p2); shape[2] = new Circle(p1, 15); ⦠void drawShapes( shape, 10 ); return 0;
}
Shape
Shape
Shape
Shape
ā¦
void drawShapes(
Shape* _shape[], int size) { for (int i = 0; i < size; i++) { // Determine object type with
// switch & accordingly call // draw() method }
}
if ( _shape[i]->getType() == āLā )
static_cast<Line*>(_shape[i])->draw();
else if ( _shape[i]->getType() == āCā ) static_cast<Circle*>(_shape[i])->draw();
ā¦
Line
Circle
Triangle
Circle
ā¦
void printArea(
Shape* _shape[], int size) { for (int i = 0; i < size; i++) { // Print shape name. // Determine object type with // switch & accordingly call // calcArea() method. }
}