计算三角形的类

class Point
{
public:
const double x, y;
Point(double x =0.0, double y=0.0 ) :x(x),y(y){}
double distanceTo(const Point& p) const
{
return sqrt((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y));
}

};

class Line
{
public:
const Point p1, p2;
Line(Point p1, Point p2) :p1(p1.x, p1.y), p2(p2.x, p2.y){}
double length()const { return p1.distanceTo(p2); }
};

class Triangle
{
public:
const Point p1, p2, p3;
Triangle(Point p1, Point p2, Point p3) :p1(p1), p2(p2), p3(p3){}
double length1()const { return Line(p1, p2).length(); }
double length2()const { return Line(p3, p2).length(); }
double length3()const { return Line(p1, p3).length(); }
double area()const
{
double s = (length1() + length2() + length3()) / 2;
return (s*(s - length1())*(s - length2())*(s - length3()));
}
};
int _tmain()
{
Triangle r(Point(0.0, 8.0), Point(5.0, 0.0),Point(0.0,0.0));
cout << r.length1() << endl;
cout << r.length2() << endl;
cout << r.length3() << endl;
cout << r.area() << endl;



}

posted @ 2016-05-05 14:45  huninglei  阅读(178)  评论(0编辑  收藏  举报