#include <iostream>
#include <cmath>
using namespace std;
class Shape
{
public:
virtual double area() const = 0;
virtual ~Shape() {}
};
class Rectangle : public Shape
{
double length, width;
public: Rectangle(double l, double w) : length(l), width(w)
{}
double area() const override
{ return length * width;
} };
class Triangle : public Shape
{
double base, height;
public:
Triangle(double b, double h) : base(b), height(h)
{}
double area() const override
{ return 0.5 * base * height;
} };
class Circle : public Shape
{
double radius;
public: Circle(double r) : radius(r)
{}
double area() const override
{
return M_PI * radius * radius;
} };
double getPositiveInput(const string &prompt)
{
double value;
while (true)
{
cout << prompt;
cin >> value;
if ([Link]() || value <= 0)
{
cout << "Invalid input. Please enter a positive number.\n"; [Link]();
[Link](numeric_limits<streamsize>::max(), '\n');
}
else
{
return value;
} } }
int main()
{
cout << "=== Area Calculation Program ===\n";
double length = getPositiveInput("Enter rectangle length: ");
double width = getPositiveInput("Enter rectangle width: ");
Shape *rect = new Rectangle(length, width);
cout << "Area of Rectangle: " << rect->area() << "\n";
delete rect;
double base = getPositiveInput("Enter triangle base: ");
double height = getPositiveInput("Enter triangle height: ");
Shape *tri = new Triangle(base, height);
cout << "Area of Triangle: " << tri->area() << "\n";
delete tri;
double radius = getPositiveInput("Enter circle radius: ");
Shape *circ = new Circle(radius);
cout << "Area of Circle: " << circ->area() << "\n";
delete circ;
return 0;
}