用c++构造一个Line类
时间: 2023-05-25 17:06:18 浏览: 63
以下是用C语言构造一个Line类的示例代码:
```
#include <stdio.h>
#include <math.h>
typedef struct {
int x;
int y;
} Point;
typedef struct {
Point start;
Point end;
} Line;
double length(Line line) {
int dx = line.end.x - line.start.x;
int dy = line.end.y - line.start.y;
return sqrt(dx*dx + dy*dy);
}
void print(Line line) {
printf("Line from (%d, %d) to (%d, %d)\n", line.start.x, line.start.y, line.end.x, line.end.y);
}
int main() {
Line line = {{0, 0}, {3, 4}};
print(line);
printf("Length of line = %f\n", length(line));
return 0;
}
```
这个Line类包含一个起点和一个终点,通过结构体实现。代码中定义了两个函数,一个是计算线段长度的`length`函数,一个是输出线段信息的`print`函数。主函数中定义了一个Line对象,并输出了线段信息和长度。
相关问题
用c++设计一个折线类
折线类(Line)可以包含以下属性和方法:
属性:
- 点数组(points):包含所有构成折线的点坐标。
方法:
- 构造函数(Line):用点数组初始化折线对象。
- 添加点(addPoint):在折线末尾添加一个点。
- 删除点(removePoint):删除指定位置的点。
- 获取点(getPoint):获取指定位置的点。
- 获取点数(getPointCount):获取折线中点的数量。
- 获取长度(getLength):计算折线的总长度。
- 获取起点(getStartPoint):获取折线的起点。
- 获取终点(getEndPoint):获取折线的终点。
- 移动折线(move):将折线平移一定的距离。
以下是折线类的示例代码:
```c
#include <stdio.h>
#include <math.h>
// 定义点结构体
typedef struct Point {
double x;
double y;
} Point;
// 折线类
typedef struct Line {
Point* points; // 点数组
int count; // 点数量
} Line;
// 构造函数:用点数组初始化折线对象
void Line_init(Line* this, Point* points, int count) {
this->points = points;
this->count = count;
}
// 在折线末尾添加一个点
void Line_addPoint(Line* this, Point point) {
this->points[this->count++] = point;
}
// 删除指定位置的点
void Line_removePoint(Line* this, int index) {
if (index >= 0 && index < this->count) {
for (int i = index; i < this->count - 1; i++) {
this->points[i] = this->points[i + 1];
}
this->count--;
}
}
// 获取指定位置的点
Point Line_getPoint(Line* this, int index) {
Point point = {0, 0};
if (index >= 0 && index < this->count) {
point = this->points[index];
}
return point;
}
// 获取折线中点的数量
int Line_getPointCount(Line* this) {
return this->count;
}
// 计算两点之间的距离
double distance(Point a, Point b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}
// 计算折线的总长度
double Line_getLength(Line* this) {
double length = 0;
for (int i = 0; i < this->count - 1; i++) {
length += distance(this->points[i], this->points[i + 1]);
}
return length;
}
// 获取折线的起点
Point Line_getStartPoint(Line* this) {
return this->points[0];
}
// 获取折线的终点
Point Line_getEndPoint(Line* this) {
return this->points[this->count - 1];
}
// 将折线平移一定的距离
void Line_move(Line* this, double dx, double dy) {
for (int i = 0; i < this->count; i++) {
this->points[i].x += dx;
this->points[i].y += dy;
}
}
int main() {
Point points[] = {{0, 0}, {1, 1}, {2, 3}, {4, 4}};
Line line;
Line_init(&line, points, 4);
printf("Length: %f\n", Line_getLength(&line));
printf("Start point: (%f, %f)\n", Line_getStartPoint(&line).x, Line_getStartPoint(&line).y);
printf("End point: (%f, %f)\n", Line_getEndPoint(&line).x, Line_getEndPoint(&line).y);
Line_move(&line, 1, 2);
printf("New start point: (%f, %f)\n", Line_getStartPoint(&line).x, Line_getStartPoint(&line).y);
return 0;
}
```
用c++写一个geometry类,可以派生circle,line,rectangle等类
好的,下面是一个可以派生circle、line、rectangle等类的C++ Geometry类示例:
```c++
// 基类 Geometry
class Geometry {
public:
// 纯虚函数,用于计算面积
virtual double area() = 0;
};
// 派生圆类 Circle
class Circle : public Geometry {
public:
// 构造函数
Circle(double radius) : radius(radius) {}
// 重载 area() 函数
double area() {
return PI * radius * radius;
}
private:
const double PI = 3.14159265358979323846;
double radius;
};
// 派生直线类 Line
class Line : public Geometry {
public:
// 构造函数
Line(double length) : length(length) {}
// 重载 area() 函数
double area() {
return 0.0;
}
private:
double length;
};
// 派生矩形类 Rectangle
class Rectangle : public Geometry {
public:
// 构造函数
Rectangle(double length, double width) : length(length), width(width) {}
// 重载 area() 函数
double area() {
return length * width;
}
private:
double length;
double width;
};
```
在这个示例中,我们定义了一个基类 Geometry,它包含一个纯虚函数 area(),用于计算面积。然后我们派生了三个类:Circle、Line和Rectangle,它们都继承了Geometry基类,并重载了area()函数,以计算特定形状的面积。
你可以在主函数中创建一个Geometry类的对象,并使用其方法计算面积。例如:
```c++
int main() {
Geometry* shape1 = new Circle(5.0);
Geometry* shape2 = new Line(10.0);
Geometry* shape3 = new Rectangle(3.0, 4.0);
std::cout << "The area of circle is " << shape1->area() << std::endl;
std::cout << "The area of line is " << shape2->area() << std::endl;
std::cout << "The area of rectangle is " << shape3->area() << std::endl;
delete shape1;
delete shape2;
delete shape3;
return 0;
}
```
输出结果为:
```
The area of circle is 78.5398
The area of line is 0
The area of rectangle is 12
```
注意,在使用完指针后,我们需要使用delete操作符释放内存。