/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 5 月 19 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: -
*问题描述: (1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生一个Circle(圆)类,增加数据成员(半径),基类成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
(4)定义友元函数,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);
*程序输出:点与圆的关系
*问题分析:
*算法设计:
*/
#include<iostream.h>
//using namespace std;
#include<cmath>
class Point
{
public:
Point(double X,double Y):x(X),y(Y){}
~Point(){}
double getX()
{
return x;
}
double getY()
{
return y;
}
friend ostream & operator << (ostream &out,Point &p);
protected:
double x,y;
};
class Circle:public Point
{
public:
Circle(double a,double b,double R):Point(a,b),r(R){}
~Circle(){}
double getR()
{
return r;
}
friend double locate(Point,Circle);
friend ostream & operator << (ostream &out,Circle &c);
private:
double r;
};
ostream & operator << (ostream &out,Point &p)
{
out<<"("<<p.getX()<<","<<p.getY()<<")"<<endl;
return out;
}
ostream & operator << (ostream &out,Circle &c)
{
out<<"("<<c.getX()<<","<<c.getY()<<")"<<" "<<"r="<<c.getR()<<endl;
return out;
}
double locate(Point p,Circle c)
{
double R,result;
R=sqrt(pow(p.getX()-c.getX(),2)+pow(p.getY()-c.getY(),2));
if(R<c.getR())
result=-1;
else if(R==c.getR())
result=0;
else
result=1;
return result;
}
int main( )
{
Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1
Point p1(1,1),p2(3,-2),p3(7,3); //分别位于c1内、上、外
cout<<"圆c1: "<<c1;
cout<<"点p1: "<<p1;
cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)?"外":((locate(p1, c1)<0)?"内":"上"))<<endl;
cout<<"点p2: "<<p2;
cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl;
cout<<"点p3: "<<p3;
cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;
return 0;
}
运行结果:
心得体会:VC++6.0编写时,需要把头文件改为iostream.h,并把using..注掉,否则出现运算符重载时二义。