
一道C++编程题!!急!!
把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类CLine,再派生出一个矩形类CRect。要求成员函数能求出两点的距离、矩形的周长和面积等。...
把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类CLine,再派生出一个矩形类CRect。要求成员函数能求出两点的距离、矩形的周长和面积等。
展开
2个回答
展开全部
#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;
}
写的比较匆忙,不是很完美,编译能够通过
希望可以帮到你
#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;
}
写的比较匆忙,不是很完美,编译能够通过
希望可以帮到你
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询