帮帮忙啊!
帮忙做下这道题,不尽感激.
①.设计一个表示平面直角坐标系的点的位置Location类,提供函数得到该点的坐标、计算两个点之间的距离。在主程序中创建两个对象A和B,按如下格式输出两个点的坐标和两个点的距离。
A(x1, y1), B(x2, y2), Distance=d
②.利用Location类,不使用继承定义直线类Line,提供得到起点和终点坐标的函数,以及计算直线长度的函数。在主程序中创建一条直线AB,按如下格式输出起点和终点坐标以及直线的长度。
AB:(x1, y1)--(x2, y2), Length=d
提示:直线由起点和终点构成,可通过两个Location对象“组成”
Line。注意利用Location的距离函数求Line长度
③.利用Location类,使用继承定义圆类Circle,圆由圆心和半径构成。提供得到圆心坐标和半径的函数、以及计算圆的周长和面积的函数。在主程序中创建两个圆A和B,按如下格式输出两个圆的圆心坐标、周长和面积,并计算和输出两个圆的圆心之间的距离。A:(x1, y1, r1), Girth=g1,Area=a1
B:(x2, y2, r2), Girth=g2,Area=a2
A(x1, y1), B(x2, y2), Distance=d
提示:通过“继承”Location作为圆心,再增加半径定义圆Circle。注意利用Location的距离函数求圆心之间的距离。
[解决办法]
拿去用吧,唉。。。。错误连篇,写的太烂了。。。。
#include "stdafx.h "
#include <math.h>
#include <iostream>
using namespace std;
#define PI 3.14
class CMyLocation
{
public:
CMyLocation();
CMyLocation(int x, int y);
~CMyLocation();
static float MyPointDistance(CMyLocation& firstCMyLocation, CMyLocation& secondCMyLocation);
void MyDisplayPointSelf();
private:
int iX;
int iY;
};
class CMyLine
{
public:
CMyLine(int x0, int y0, int x1, int y1);
~CMyLine();
void MyDisplayLineSelf();
float MyLineLength();
private:
int iX0;
int iX1;
int iY0;
int iY1;
CMyLocation iStartpoint;
CMyLocation iEndpoint;
};
class CMyCircle : public CMyLocation
{
public:
CMyCircle(int x, int y, int r);
~CMyCircle();
void MyDisplayCircleSelf();
static float MyCircleDistance(CMyCircle& firstCMyCircle, CMyCircle& secondCMyCircle);
private:
int iCx;
int iCy;
int iR;
CMyLocation iCentralpoint;
};
int _tmain(int argc, _TCHAR* argv[])
{
int x,y;
cout < < "input the position of first point " < < endl;
cin > > x > > y;
CMyLocation A(x,y);
cout < < "input the position of second point " < < endl;
cin > > x > > y;
CMyLocation B(x,y);
A.MyDisplayPointSelf();
cout < < ", ";
B.MyDisplayPointSelf();
cout < < ", Distance = " < < CMyLocation::MyPointDistance(A, B) < < endl;
cout < < "input two point postion of section " < < endl;
int a,b,c,d;
cin > > a > > b > > c > > d;
CMyLine vsection(a, b, c, d);
vsection.MyDisplayLineSelf();
cout < < ", Distance = " < < vsection.MyLineLength() < < endl;
int cx,cy, r;
cout < < "input the first circle parameters: " < < endl;
cin > > cx > > cy > > r;
CMyCircle CA(cx, cy, r);
cout < < "input the second circle parameters: " < < endl;
cin > > cx > > cy > > r;
CMyCircle CB(cx, cy, r);
CA.MyDisplayCircleSelf();
CB.MyDisplayCircleSelf();
cout < < ",Distance = " < < CMyCircle::MyCircleDistance(CA, CB) < < endl;
system( "pause ");
return 0;
}
CMyLocation::CMyLocation()
{
iX = 0;
iY = 0;
}
CMyLocation::CMyLocation(int x, int y)
{
iX = x;
iY = y;
}
CMyLocation::~CMyLocation(){};
float CMyLocation::MyPointDistance(CMyLocation& firstCMyLocation, CMyLocation& secondCMyLocation)
{
return sqrt(float((firstCMyLocation.iX - secondCMyLocation.iX) * (firstCMyLocation.iX - secondCMyLocation.iX) +
(firstCMyLocation.iY - secondCMyLocation.iY) * (firstCMyLocation.iY - secondCMyLocation.iY)));
}
void CMyLocation::MyDisplayPointSelf()
{
cout < < "( " < < iX < < ", " < < iY < < ") ";
}
CMyLine::CMyLine(int x0, int y0, int x1, int y1):iStartpoint(x0, y0),iEndpoint(x1,y1)
{
iX0 = x0;
iX1 = x1;
iY0 = y0;
iY1 = y1;
}
CMyLine::~CMyLine(){};
float CMyLine::MyLineLength()
{
return CMyLocation::MyPointDistance(iStartpoint, iEndpoint);
}
void CMyLine::MyDisplayLineSelf()
{
cout < < "( " < < iX0 < < ", " < < iY0 < < ") " < < "---- ";
cout < < "( " < < iX1 < < ", " < < iY1 < < ") ";
}
CMyCircle::CMyCircle(int x, int y, int r):iCx(x),iCy(y),iR(r),iCentralpoint(x,y)
{
}
CMyCircle::~CMyCircle(){};
void CMyCircle::MyDisplayCircleSelf()
{
cout < < "( " < < iCx < < ", " < < iCy < < ", " < < iR < < ") ";
cout < < "Girth = " < < 2 * PI * iR < < ", " < < "Area = " < < PI * iR * iR;
}
float CMyCircle::MyCircleDistance(CMyCircle& firstCMyCircle, CMyCircle& secondCMyCircle)
{
return MyPointDistance(firstCMyCircle.iCentralpoint, secondCMyCircle.iCentralpoint);
}