定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area
时间: 2024-05-06 13:20:39 浏览: 100
矩形类及派生类,算面积、体积
```
#include <iostream>
using namespace std;
class Point {
protected:
double x, y;
public:
Point(double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
virtual double Area() = 0;
};
class Rectangle : public Point {
private:
double length, width;
public:
Rectangle(double x = 0, double y = 0, double length = 0, double width = 0) : Point(x, y) {
this->length = length;
this->width = width;
}
double Area() {
return length * width;
}
};
class Circle : public Point {
private:
double radius;
public:
Circle(double x = 0, double y = 0, double radius = 0) : Point(x, y) {
this->radius = radius;
}
double Area() {
return 3.1415926 * radius * radius;
}
};
int main() {
Point* p;
Rectangle r(0, 0, 3, 4);
Circle c(0, 0, 5);
p = &r;
cout << "Rectangle Area: " << p->Area() << endl;
p = &c;
cout << "Circle Area: " << p->Area() << endl;
return 0;
}
```
阅读全文