把描述直角坐标系上的一个点的类作为基类派生出描述一条直线的泪和一个描述三角形的类定义成员函数,要求两点间的距离和三角形的面积

把描述直角坐标系上的一个点的类作为基类派生出描述一条直线的泪和一个描述三角形的类定义成员函数,要求两点间的距离和三角形的面积


在这里插入图片描述

#include<iostream>
#include<string>
#include<cmath>
using namespace std;

class Point
{
protected:
    int x1, y1;
public:
    Point(int a, int b)
    {
        x1 = a; y1 = b;
    }
};

class Line :public Point
{
protected:
    int x2, y2;
public:
    Line(int a, int b, int c, int d) :Point(a, b), x2(c), y2(d) {}
};

class Triangle :public Line
{
protected:
    int x3, y3;
    double area;
public:
    Triangle(int a, int b, int c, int d, int e, int f) :Line(a, b, c, d), x3(e), y3(f) {}
    void f() {
        double x, y, z, s;
        x = sqrt((double)(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
        y = sqrt((double)(x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
        z = sqrt((double)(x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
        s = (x + y + z) / 2;
        area = sqrt(s * (s - x) * (s - y) * (s - z));
    }

    void print()
    {
        cout << "(" << x1 << "," << y1 << ")" << "(" << x2 << "," << y2 << ")" << "(" << x3 << "," << y3 << ")" << endl;
        cout << "area=" << area << endl;
    }
};


int main()
{
    int a[6];
    for (int i = 0; i < 6; i++)
        cin >> a[i];
    Triangle tri(a[0], a[1], a[2], a[3], a[4], a[5]);
    tri.f();
    tri.print();
    return 0;
}

好的,让我来帮你完成这个任务。 首先,我们需要定义一个描述点的 Point: ```python class Point: def __init__(self, x, y): self.x = x self.y = y ``` 接下来,我们创建 Point 的派生 Line,用于描述一条直线。由于一条直线可以由两个点确定,因此我们在 Line 的构造函数中传入两个 Point 型的对象: ```python class Line(Point): def __init__(self, point1, point2): self.point1 = point1 self.point2 = point2 ``` 现在,我们可以定义成员函数两点距离。我们可以使用勾股定理来计算两点距离: ```python class Line(Point): def __init__(self, point1, point2): self.point1 = point1 self.point2 = point2 def distance(self): return ((self.point1.x - self.point2.x) ** 2 + (self.point1.y - self.point2.y) ** 2) ** 0.5 ``` 接下来,我们创建描述三角形 Triangle。由于一个三角形可以由三个点确定,因此我们在 Triangle 的构造函数中传入三个 Point 型的对象: ```python class Triangle(Point): def __init__(self, point1, point2, point3): self.point1 = point1 self.point2 = point2 self.point3 = point3 ``` 现在,我们可以定义成员函数三角形面积。我们可以使用海伦公式来计算三角形面积: ```python class Triangle(Point): def __init__(self, point1, point2, point3): self.point1 = point1 self.point2 = point2 self.point3 = point3 def distance(self, point1, point2): return ((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2) ** 0.5 def area(self): a = self.distance(self.point1, self.point2) b = self.distance(self.point2, self.point3) c = self.distance(self.point3, self.point1) s = (a + b + c) / 2 return (s * (s - a) * (s - b) * (s - c)) ** 0.5 ``` 现在,我们已经定义描述点、直线三角形,并且实现了求两点距离三角形面积成员函数。下面是完整的代码: ```python class Point: def __init__(self, x, y): self.x = x self.y = y class Line(Point): def __init__(self, point1, point2): self.point1 = point1 self.point2 = point2 def distance(self): return ((self.point1.x - self.point2.x) ** 2 + (self.point1.y - self.point2.y) ** 2) ** 0.5 class Triangle(Point): def __init__(self, point1, point2, point3): self.point1 = point1 self.point2 = point2 self.point3 = point3 def distance(self, point1, point2): return ((point1.x - point2.x) ** 2 + (point1.y - point2.y) ** 2) ** 0.5 def area(self): a = self.distance(self.point1, self.point2) b = self.distance(self.point2, self.point3) c = self.distance(self.point3, self.point1) s = (a + b + c) / 2 return (s * (s - a) * (s - b) * (s - c)) ** 0.5 ``` 你可以通过以下代码测试上述的功能: ```python # 创建两个点 p1 = Point(0, 0) p2 = Point(3, 4) # 创建一条直线 line = Line(p1, p2) # 求两点距离 print(line.distance()) # 输 5.0 # 创建一个三角形 p3 = Point(5, 0) triangle = Triangle(p1, p2, p3) # 求三角形面积 print(triangle.area()) # 输 10.0 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值