实验题目2 定义一个Point类,用来产生平面上的点对象。两点决定一条线段,即线段由点构成。因此,Line类使用Point类的对象作为数据成员,然后在Line类的构造函数中求出线段的长度。 要求: ① 定义两个类Point和Line; ② 能够自己输入点的坐标(x,y),有相应的提示语句,如:‘please input xxx’; ③ 可以输出线段的长度 ④ 必须基于面向对象的方式实现,不可直接用面向过程的方式实现 二. 算法的设计思路
时间: 2023-06-06 19:05:33 浏览: 188
1. Point类的设计:
属性:x坐标、y坐标;
方法:构造函数,用于初始化点对象;
2. Line类的设计:
属性:起点、终点;
方法:
(1) 构造函数:传入起点和终点对象,计算出线段长度;
(2) 计算线段长度的方法。
算法流程:
1. 定义Point类,包含x、y属性和构造函数;
2. 定义Line类,包含起点和终点属性和构造函数,构造函数中计算并记录线段长度;
3. 用户输入起点和终点的坐标,创建对应的Point对象;
4. 使用创建的Point对象创建Line对象;
5. 输出线段长度。
注意事项:
1. 提供适当的输入提示语句;
2. 计算线段长度时可以使用勾股定理公式;
3. 确保对象的合法性,如起点和终点不能重合。
相关问题
要求先定义一个point类,用来产生平面上的点对象。两点决定一条线段,即线段由点构成。因此,line类使用point类的对象作为数据成员,然后在line类的构造函数中求出线段的长度
以下是定义一个point类和line类的示例代码:
```cpp
#include <iostream>
#include <cmath>
using namespace std;
class point{
public:
point(double init_x = 0, double init_y = 0): x(init_x), y(init_y){}
double get_x() const { return x; }
double get_y() const { return y; }
private:
double x, y;
};
class line{
public:
line(const point& p1, const point& p2): start(p1), end(p2){
double dx = end.get_x() - start.get_x();
double dy = end.get_y() - start.get_y();
length = sqrt(dx*dx + dy*dy);
}
double get_length() const { return length; }
private:
point start, end;
double length;
};
int main(){
point p1(1, 2);
point p2(4, 6);
line l(p1, p2);
cout << "The length of the line is " << l.get_length() << endl;
return 0;
}
```
在这个示例中,point类表示平面上的一个点,有两个私有数据成员x和y,分别表示点的横坐标和纵坐标。构造函数可以接受两个参数,分别表示点的横坐标和纵坐标。get_x()和get_y()成员函数分别返回点的横坐标和纵坐标。
line类使用point类的对象作为数据成员,表示一个线段。在构造函数中,我们需要计算出线段的长度。先计算出线段的横坐标和纵坐标的差值,然后使用勾股定理计算出长度。get_length()成员函数返回线段的长度。
在main函数中,我们创建了两个点p1和p2,然后使用它们创建了一个线段l。最后输出线段的长度。
1.使用对象成员构成新类。 要求先定义一个Point类,用来产生平面上的点对象。两点决定一条线段,即线段由点构成。因此,Line类使用Point类的对象作为数据成员,然后在Line类的构造函数中求出线段的长度。
下面是一个使用 Point 类对象作为数据成员的 Line 类的示例代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y) {}
double getX() const { return x; }
double getY() const { return y; }
};
class Line {
private:
Point p1, p2;
double len;
public:
Line(const Point& p1, const Point& p2) : p1(p1), p2(p2) {
double dx = p1.getX() - p2.getX();
double dy = p1.getY() - p2.getY();
len = sqrt(dx * dx + dy * dy);
}
double getLen() const { return len; }
};
int main() {
Point p1(1, 1);
Point p2(4, 5);
Line l(p1, p2);
cout << "Length of line: " << l.getLen() << endl;
return 0;
}
```
在上面的代码中,Point 类代表平面上的点,包含两个数据成员 x 和 y,表示点的横纵坐标。Line 类使用两个 Point 类对象作为数据成员,分别表示线段的两个端点。在 Line 类的构造函数中,计算出线段的长度 len。getLen() 函数用于获取线段的长度。在 main() 函数中创建两个 Point 对象和一个 Line 对象,并输出线段的长度。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)