Shape类派生了Circle类和Rectangle类,请阅读主函数代码,完成Shape类、Circle类、Rectangle类的定义
时间: 2024-05-12 22:20:57 浏览: 105
```
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
public:
virtual double Area() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double Area() const {
return M_PI * radius * radius;
}
};
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double Area() const {
return width * height;
}
};
int main() {
Shape* s1 = new Circle(5);
Shape* s2 = new Rectangle(4, 6);
cout << "Circle area: " << s1->Area() << endl;
cout << "Rectangle area: " << s2->Area() << endl;
delete s1;
delete s2;
return 0;
}
```
阅读全文