1·题目介绍
2·参考程序
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
protected:
int x1;
int y1;
public:
Point(int a, int b)
{
x1 = a;
y1 = b;
}
};
class Line:public Point
{
protected:
int x2;
int y2;
public:
Line(int a, int b, int c, int d) :Point(a, b)
{
x2 = c;
y2 = d;
}
double fa()
{
double s;
s = sqrt((x2 - x1) * (x2 - x1)+(y2 - y1) * (y2 - y1));
return s;
}
};
class Triangle:public Line
{
private:
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()
{
Line l1(x2, y2, x3, y3);
Line l2(x1, y1, x3, y3);
Line l3(x1, y1, x2, y2);
double x, y, z;
x = l1.fa();
y = l2.fa();
z = l3.fa();
double s;
s = (x + y + z) / 2.0;
area = sqrt(s * (s - x) * (s - y) * (s - z));
}
void print()
{
cout << x1 << '\t' << y1 << endl;
cout << x2 << '\t' << y2 << endl;
cout << x3 << '\t' << y3 << endl;
cout << "面积是:" << area << endl;
}
};
int main()
{
Triangle tri(1, 1, 4, 1, 4, 5);
tri.f();
tri.print();
return 0;
}
3·心得收获
a·若在派生类中使用与基类相同的功能时,尽可能的去调用函数,不要再写同样的函数