问题及代码
/*
ALL rights reserved.
*文件名称: 初学对象12
作者:李长鸿
*完成时间:2015.5.27
*问题描述: 点与圆的继承
*/
#include <iostream>
#include<Cmath>
using namespace std;
class Point
{
double x,y;
public:
Point(double a=0,double b=0):x(a),y(b) {}
void setp()
{
cout<<"请输入点的坐标:";
char a,b,c;
cin>>a>>x>>b>>y>>c;
while(a!='('||b!=','||c!=')')
{
cout<<"输入有误,请再次输入:";
cin>>a>>x>>b>>y>>c;
}
}
inline double getx()
{
return x;
}
inline double gety()
{
return y;
}
void printp()
{
cout<<"("<<x<<","<<y<<"); ";
}
};
class Circle:public Point
{
double r;
public:
Circle(double a=0,double b=0,double c=1):Point(a,b),r(c) {}
inline double getr()
{
return r;
}
friend istream &operator>>(istream&in,Circle&c);
friend ostream &operator<<(ostream&out,Circle&c);
double area();
};
istream &operator>>(istream &in,Circle &c)
{
c.setp();
cout<<"请输入半径:";
in>>c.r;
while(c.r<=0)
{
in>>c.r;
}
return in;
}
double Circle::area()
{
return 3.14*r*r;
}
ostream& operator<<(ostream &out,Circle &c)
{
cout<<"圆心坐标为:";
c.printp();
cout<<"半径为:"<<c.r<<"; ";
cout<<"面积为:"<<c.area();
return out;
}
int locate(Point p,Circle c)
{
double h=sqrt((p.getx()-c.getx())*(p.getx()-c.getx())+(p.gety()-c.gety())*(p.gety()-c.gety()));
if(h==c.getr())
return 0;
if(h<c.getr())
return -1;
if(h>c.gety())
return 1;
}
int main()
{
Circle c1(7,7,7);
Point p1(7,7);
cout<<"p1=";
p1.printp();
cout<<endl;
cout<<"圆为:";
cout<<c1;
cout<<endl;
p1.setp();
cin>>c1;
cout<<c1;
cout<<endl;
cout<<"点";
p1.printp();
cout<<"在";
cout<<c1;
cout<<"的圆";
if(locate(p1,c1)==0)
{
cout<<"上"<<endl;
}
if(locate(p1,c1)>0)
{
cout<<"外"<<endl;
}
if(locate(p1,c1)<0)
{
cout<<"内"<<endl;
}
return 0;
}
总结:写<< >>的重载时,格式的小细节未注意好,operator前的&忘了,out , in前的&也忘了,结果系统就给来个内存什么的问题。唉