一道C++编程题!!急!!

把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类CLine,再派生出一个矩形类CRect。要求成员函数能求出两点的距离、矩形的周长和面积等。... 把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类CLine,再派生出一个矩形类CRect。要求成员函数能求出两点的距离、矩形的周长和面积等。 展开
 我来答
流星梦阳
2011-10-15 · TA获得超过468个赞
知道小有建树答主
回答量:134
采纳率:0%
帮助的人:180万
展开全部
#include <iostream>
#include <math.h> //开方头文件
using namespace std;

class CPoint
{
public:
int x;
int y;
void setPoint(int x,int y) //设置点
{
this->x=x;
this->y=y;
}
};

class CLine : public CPoint
{
public:
CPoint point1;
CPoint point2;

void setLine(CPoint point1,CPoint point2) //设直线段
{
this->point1=point1;
this->point2=point2;
}
float getLong()
{
float long1;
long1=sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)); //两点线段长度
return long1;
}
};

class CRet : public CLine
{
public:
CLine line1;
CLine line2;

void setRet(CLine line1,CLine line2) //根据两直线画矩形
{
this->line1=line1;
this->line2=line2;
}
float getSqrt()
{
float retsqrt;
retsqrt=line1.getLong()*line2.getLong(); //面积
return retsqrt;
}
float getPer()
{
float perimeter;
perimeter=2*line1.getLong()+2*line2.getLong(); //周长
return perimeter;
}

};

int main()
{
CRet ret; //创建矩形对象

ret.line1.point1.setPoint(1,0);
ret.line1.point2.setPoint(0,1); //确定第一条直线
ret.line2.point1.setPoint(1,0);
ret.line2.point2.setPoint(0,1); //确定第二条直线
cout<<"The first line long:\t"<<ret.line1.getLong()<<endl;
cout<<"The second line long:\t"<<ret.line2.getLong()<<endl;
cout<<"The ret sqrt:\t\t"<<ret.getSqrt()<<endl;
cout<<"The ret perimeter:\t"<<ret.getPer()<<endl;

return -1;
}

写的比较匆忙,不是很完美,编译能够通过
希望可以帮到你
huaaixue
2011-10-15 · TA获得超过524个赞
知道小有建树答主
回答量:1070
采纳率:0%
帮助的人:514万
展开全部
这又不难。。。就是考面向对象而已
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式