设有一个点类Point的定义如下: Point { public: Point() {x = 0; y = 0; } Point(double xv,double yv) {x = xv;y = yv;} Point(Point& pt) { x = pt.x; y = pt.y; } double getx() { return x; } double gety() { return y; } double Area() { return 0; } void Show() { cout<<"x="<<x<<' '<<"y="<<y<<endl; } private: double x,y; }; 编写程序,以点point类为基类,派生出矩形类Rectangle和圆类Circle。矩形由左上角的顶点和长、宽定义。圆由圆心和半径定义。派生类Circle中新增的成员函数position(Point &pt)用于判断任一坐标点是在圆内、还是在圆的边缘上,还是在圆外。
时间: 2023-06-25 08:06:40 浏览: 147
以下是基于点类Point的派生类Rectangle和Circle的定义:
```cpp
class Rectangle : public Point {
public:
Rectangle(): Point(), length(0), width(0) {}
Rectangle(double xv, double yv, double l, double w): Point(xv, yv), length(l), width(w) {}
Rectangle(Point& pt, double l, double w): Point(pt), length(l), width(w) {}
double getLength() { return length; }
double getWidth() { return width; }
double Area() { return length * width; }
void Show() {
cout << "Left top corner: ";
Point::Show();
cout << "Length: " << length << " Width: " << width << endl;
}
private:
double length, width;
};
class Circle : public Point {
public:
Circle(): Point(), radius(0) {}
Circle(double xv, double yv, double r): Point(xv, yv), radius(r) {}
Circle(Point& pt, double r): Point(pt), radius(r) {}
double getRadius() { return radius; }
double Area() { return PI * radius * radius; }
void Show() {
cout << "Center: ";
Point::Show();
cout << "Radius: " << radius << endl;
}
int position(Point& pt) {
double distance = sqrt(pow(pt.getx()-getx(), 2) + pow(pt.gety()-gety(), 2));
if (distance < radius) return 1;
else if (distance == radius) return 0;
else return -1;
}
private:
double radius;
const double PI = 3.14159265358979323846264338327;
};
```
其中,Rectangle类表示矩形,包含左上角的顶点和长宽,其派生自Point类;Circle类表示圆,包含圆心和半径,其也派生自Point类,新增了成员函数position,用于判断任一坐标点在圆内、圆的边缘上或圆外。
阅读全文