point点类派生rectangle类
时间: 2023-05-03 16:00:29 浏览: 175
这个问题是以十六进制编码的字符串形式提出的,需要先将其解码成可读中文。解码后问题为“点类派生Rectangle类”,回答如下:
点类派生Rectangle类是指在面向对象编程中,通过继承点类的属性和方法,扩展定义一个矩形类。矩形类拥有点类的属性和方法,同时增加了自身的特点,如长、宽等属性和计算面积、周长等方法。这样能够提高代码的重用性和可扩展性。
相关问题
定义一个Point类,派生出Rectangle类和Circle类
好的,以下是定义一个Point类,派生出Rectangle类和Circle类的C++代码:
```c++
class Point {
private:
double x;
double y;
public:
Point(double x, double y) : x(x), y(y) {}
double getX() const { return x; }
double getY() const { return y; }
};
class Rectangle : public Point {
private:
double width;
double height;
public:
Rectangle(double x, double y, double width, double height) : Point(x, y), width(width), height(height) {}
double getWidth() const { return width; }
double getHeight() const { return height; }
};
class Circle : public Point {
private:
double radius;
public:
Circle(double x, double y, double radius) : Point(x, y), radius(radius) {}
double getRadius() const { return radius; }
};
```
在这个代码中,Point类表示平面上的一个点,包含x和y坐标。Rectangle类和Circle类都派生自Point类,表示平面上的一个矩形和一个圆形。Rectangle类包含width和height两个属性,表示矩形的宽和高;Circle类包含radius属性,表示圆形的半径。
定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area
```
#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;
}
```
阅读全文