c++:实验题1

  1. ``根据点的类构造线三角形类,并测试
    【输入形式】输入三角形的3个坐标点
    【输出形式】三角形的3个坐标点、3边长及周长和面积
/*实验七 类和对象(3)---点、线、三角形*/

	

//根据点的定义,写出线段和三角形的定义,并通过主程序进行验证
//mypoint.h
class  myPoint
{
   
     public:
        myPoint();
        myPoint(double x,double y);
        double  getX();
        double  getY();
     private:
        double mX,mY;
};
#include<iostream>
#include<cmath>
using namespace std;
myPoint::myPoint(double x,double y)
{
   
      mX=x;
	  mY=y;
}
double myPoint::getX()
{
   
    return mX;
}
double myPoint::getY()
{
   
	return mY;
 } 
class Line{
   
	private:
		myPoint a,b;
	public:
		Line(myPoint a,myPoint b):a(a),b(b){
   }; 组合类的构造函数对内前对象成员的初始化必须采用初始化列表形式 
		double GetDistance()
		{
   
		   double x=double(a.getX()-b.getX());
		   double y=double(a.getY()-b.getY());
		   return (double) sqrt(x*x+y*y);
	    }
}; 
//end Line
class Triangle
{
   
	private:
		double circle;
		double area;
	public:
		Triangle(myPoint i,myPoint k,myPoint j);
		double getGirth()
		{
   
			return circle;
		}
		double getArea()
		{
   
		   return area;
	    }
};
Triangle::Triangle(myPoint i,myPoint k,myPoint j)
{
   
	Line a(i,k),b(k,j),c(i,j);
	circle=a.GetDistance()+b.GetDistance()+c.GetDistance();
	double t=circle/2;
	area=sqrt(t*(t-a.GetDistance())*(t-b.GetDistance())*(t-c.GetDistance())
  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值