C++:继承3(是圆内、圆外还是圆上)

来源:互联网 发布:淘宝接单app软件 编辑:程序博客网 时间:2024/06/06 01:34

继承3(是圆内、圆外还是圆上)

Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByte
Total Submit:272 Accepted:200

Description

定义点类为基类,其数据成员x和y为私有成员。再定义圆类继承点类,该点为圆的圆心,新增数据成员圆的半径。

在圆类中定义成员函数,其形式参数为一个点,判断该点是在圆内部、圆外还是圆上。

Input

包含多组测试例, 每组数据的第1行是圆心的坐标,第2行是圆的半径,第1行是另外一个点的坐标。

Output

如果点在圆内部输出“in”,点在圆外输出“out”,点在在圆上输出“on”。

Sample Input

0 0
8
6 8
0 0
10
6 8
0 0
12
6 8

Sample Output

out
on
in

Hint

不要使用sqrt函数,通过比较距离的平方与半径的平方来避免误差。


#include<iostream>using namespace std;class Point{protected:    int x,y;public:    Point(int a,int b)    {        x=a;        y=b;    }};class Circle:public Point{     int x1,y1,r;public:  Circle(int a,int b,int c,int d,int e):Point(a,b)  {      x1=d;      y1=e;      r=c;  }  void disp()  {      double s,s1,m;      s=(x-x1)*(x-x1)+(y-y1)*(y-y1);      s1=r*r;      m=s-s1;  if(m>0)      cout<<"out"<<endl;  else  {      if(m==0)          cout<<"on"<<endl;      else       cout<<"in"<<endl;}  } };int main(){    int a,b,c,d,e;  while(cin>>a>>b>>c>>d>>e)  {    Circle w(a,b,c,d,e);w.disp();  }   return 0;}
1 0