把描述直角座標系上的一個點的類作爲基類派生出描述一條直線的淚和一個描述三角形的類定義成員函數,要求兩點間的距離和三角形的面積

把描述直角座標系上的一個點的類作爲基類派生出描述一條直線的淚和一個描述三角形的類定義成員函數,要求兩點間的距離和三角形的面積


在這裏插入圖片描述

#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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章