point类的设计 C++

(1)定义Point类,能够设置、获取并打印出点的X、Y坐标,要求声明在Point.h中,实现在Point.cpp中。(2)定义Circle类,能够设置、获取并打印出圆心... (1)定义Point类,能够设置、获取并打印出点的X、Y坐标,要求声明在Point.h中,实现在Point.cpp中。
(2)定义Circle类,能够设置、获取并打印出圆心(X、Y坐标)和半径,并能够判断两个圆的位置是相交、
想离还是相切。同样声明在Circle.h中,实现在Circle.cpp中。
(3)在main.cpp中定义main函数,并完成上述要求的功能。
----------------------------------
分别写出Point.h,Point.cpp,Circle.h,Circle.cpp,main.cpp的内容。
展开
 我来答
库鲁滋·韦伯ea3
2010-10-30 · TA获得超过440个赞
知道小有建树答主
回答量:245
采纳率:0%
帮助的人:0
展开全部
如果把point 类 和circle 类分开来写是不明智的写法,因为circle类和point类拥有共同的特点,都有一个点,circle类只是多了半径出来,所以circle类应该是point的派生类。于是头文件point.h如下:
#ifndef POINT_H_
#define POINT_H_
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
private:
double x,y;
public:
Point(double a,double b):x(a),y(b){}
double Get_Point_X()const{return x;};
double Get_Point_Y()const{return y;};
void Reset_Point(double c,double d){x=c;y=d;};
void Print_Point(){cout<<"X:"<<Get_Point_X()<<",Y:"<<Get_Point_Y()<<endl;};
};
class Circle:public Point
{
private:
double r;
public:
Circle(double a,double b,double R):Point(a,b),r(R){}
double Get_Circle_r()const{return r;};
void Print_Circle(){cout<<"circle is X:"<<Get_Point_X()<<",Y:"<<Get_Point_Y()<<",r:"<<Get_Circle_r()<<endl;};
friend double Distance_Point(Circle &cl1,Circle &cl2);
friend double Distance_R(Circle &cl1,Circle &cl2);
friend double Get_Distance_X(Circle &cl1,Circle &cl2);
friend double Get_Distance_Y(Circle &cl1,Circle &cl2);
};
double Get_Distance_X(Circle &cl1,Circle &cl2)
{
double X=cl1.Get_Point_X()-cl2.Get_Point_X();
return X*X;
}
double Get_Distance_Y(Circle &cl1,Circle &cl2)
{
double Y=cl1.Get_Point_Y()-cl2.Get_Point_Y();
return Y*Y;
}
double Distance_Point(Circle &cl1,Circle &cl2)
{
double distance=Get_Distance_X(cl1,cl2)+Get_Distance_Y(cl1,cl2);
return sqrt(distance);
}
double Distance_R(Circle &cl1,Circle &cl2)
{
double a=cl1.Get_Circle_r()+cl2.Get_Circle_r();
return a;
}
#endif /* POINT_H_ */
主体文件point.cpp如下:
#include"point.h"
int main(void)
{
int x,y,r;
cout<<"input the point X:";
cin>>x;
cout<<"input the point Y:";
cin>>y;
Point po(x,y);
cout<<"the point is:"<<endl;
po.Print_Point();
cout<<"now you can reset it:"<<endl;
cout<<"input the new point X:";
cin>>x;
cout<<"input the new point Y:";
cin>>y;
po.Reset_Point(x,y);
cout<<"the new point is:"<<endl;
po.Print_Point();
cout<<"input the circle1 point X:";
cin>>x;
cout<<"input the circle1 point Y:";
cin>>y;
cout<<"input the circle1 point r:";
cin>>r;
Circle cl1(x,y,r);
cl1.Print_Circle();
cout<<"input circle2 point X:";
cin>>x;
cout<<"input circle2 point Y:";
cin>>y;
cout<<"input circle2 point r:";
cin>>r;
Circle cl2(x,y,r);
cl2.Print_Circle();
cout<<"circle1 and circle2 are ";
if(Distance_Point(cl1,cl2)==Distance_R(cl1,cl2))
{
cout<<"tangent"<<endl;
}
else if(Distance_Point(cl1,cl2)<Distance_R(cl1,cl2))
cout<<"cross"<<endl;
else
cout<<"away"<<endl;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式