1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Shape { public: virtual double area() const = 0; };
class Circle : public Shape { public: double radius; Circle(double r) : radius(r) {} double area() const override { return 3.14 * radius * radius; } };
class Rectangle : public Shape { public: double width, height; Rectangle(double w, double h) : width(w), height(h) {} double area() const override { return width * height; } };
int main() { Shape* shapes[] = {new Circle(5), new Rectangle(4, 6)}; for (auto shape : shapes) { std::cout << "Area: " << shape->area() << std::endl; } for (auto shape : shapes) { delete shape; } return 0; }
|