C++点类派生三角形求面积,周长,构造析构函数

仅供参考

#include <iostream>
#include <math.h>
using namespace std;
class myPoint {
public:
	myPoint(double x0 = 0.0, double y0 = 0.0) :x(x0), y(y0) {}
	myPoint(myPoint &np) :x(np.x), y(np.y) {}
	double GetX() { return x; }
	double GetY() { return y; }
	void SetX(double x0) { x = x0; }
	void SetY(double y0) { x = y0; }
	void SetPoint(double x0, double y0) { x = x0; y = y0; }
	void SetPoint(myPoint &np) { x = np.x; y = np.y; }
	double  GetLength(myPoint p) {
		return sqrt((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y));
	}
	void Printit() { cout << " (" << x << "," << y << ") "; }
private:
	double x, y;
};
class Triangle {
public:
	double GetLength(myPoint p1, myPoint p2, myPoint p3)
	{
		return (p1.GetLength(p2) + p2.GetLength(p3) + p3.GetLength(p1));
	}
	double GetArea(myPoint p1, myPoint p2, myPoint p3)
	{
		double a, b, c, s, area;
		a = p1.GetLength(p2);
		b = p2.GetLength(p3);
		c = p3.GetLength(p1);
		s = 0.5*(a + b + c);
		area = sqrt(s*(s - a)*(s - b)*(s - c));
		return area;
	}
};
int main()
{
	myPoint a(0, 0), b(4, 0), c(2, 3);
	Triangle t;
	cout << "area=" << t.GetArea(a, b, c) << endl;
	cout << "length=" << t.GetLength(a, b, c) << endl;
	getchar();
	return 0;
}


结果:
Alt

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值