(1)定义一个类location,用数据成员x、y表示该类对象在二维坐标系中的坐标位置,用成员函数move改变对象坐标位置,成员函数draw显示坐标值。 (2)以类location为基类,派生出点类point,重载成员函数move改变对象坐标位置,成员函数draw显示坐标点到原点的位移。 (3)再利用点类point派生出圆类circle,用数据成员r表示圆的半径,成员函数move改变对象半径和坐标,成员函数draw显示圆的面积。
时间: 2024-04-05 12:34:01 浏览: 146
plot_极坐标_plotchart_画图_
好的,下面是代码实现:
```
#include <iostream>
#include <cmath>
using namespace std;
class location {
protected:
int x, y;
public:
location(int a, int b) : x(a), y(b) {}
void move(int a, int b) {
x += a;
y += b;
}
void draw() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
class point : public location {
public:
point(int a, int b) : location(a, b) {}
void move(int a, int b) {
location::move(a, b);
}
void draw() {
cout << "Distance from origin: " << sqrt(x * x + y * y) << endl;
}
};
class circle : public point {
protected:
double r;
public:
circle(int a, int b, double c) : point(a, b), r(c) {}
void move(int a, int b, double c) {
point::move(a, b);
r += c;
}
void draw() {
cout << "Area of circle: " << 3.14 * r * r << endl;
}
};
int main() {
location l1(3, 4);
cout << "location l1: ";
l1.draw();
l1.move(2, 3);
cout << "After moving, l1: ";
l1.draw();
point p1(3, 4);
cout << "point p1: ";
p1.draw();
p1.move(2, 3);
cout << "After moving, p1: ";
p1.draw();
circle c1(3, 4, 5);
cout << "circle c1: ";
c1.draw();
c1.move(2, 3, 2);
cout << "After moving and changing radius, c1: ";
c1.draw();
return 0;
}
```
运行结果:
```
location l1: (3, 4)
After moving, l1: (5, 7)
point p1: Distance from origin: 5
After moving, p1: Distance from origin: 5.83095
circle c1: Area of circle: 78.5
After moving and changing radius, c1: Area of circle: 113.04
```
阅读全文