Create a program that uses polymorphism to handle different shapes and calculate their areas.
HardProblem Statement
Create a program that uses polymorphism to handle different shapes and calculate their areas.
Sample Input
Shape* shapes[2]; shapes[0] = new Circle(3); shapes[1] = new Rectangle(5, 10);
Sample Output
Area of circle: 28.2743 Area of rectangle: 50
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
#include <cmath>
#include <iostream>
class Shape {
public:
virtual double area() = 0; // Pure virtual function
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r);
double area();
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h);
double area();
};
int main() {
// Logic to demonstrate polymorphism
return 0;
}
Input
Shape* shapes[2]; shapes[0] = new Circle(3); shapes[1] = new Rectangle(5, 10);
Expected Output
Area of circle: 28.2743 Area of rectangle: 50