/*(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
(4)定义友元函数int locate,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);
在项目1的基础上,在圆类上重载关系运算符(6种),使之能够按圆的面积比较两个圆的大小。
自编main函数完成测试。
*/
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(double xx,double yy):x(xx),y(yy){}
protected:
double x;
double y;
};
class Circle:public Point
{
public:
Circle(double xx,double yy,double R):Point(xx,yy),r(R){}
friend ostream &operator<<(ostream &,Circle &);
double area();
bool operator>(const Circle &);
bool operator<(const Circle &);
bool operator>=(const Circle &);
bool operator<=(const Circle &);
bool operator==(const Circle &);
bool operator!=(const Circle &);
protected:
double r;
};
ostream &operator<<(ostream &output,Circle &c)
{
output<<"为("<<c.x<<", "<<c.y<<"), r="<<c.r<<" ";
return output;
}
double Circle::area()
{
double s;
s=3*r*r;
return s;
}
bool Circle::operator>(const Circle &c)
{
return (r-c.r)>0;
}
bool Circle::operator<(const Circle &c)
{
return (r-c.r)<0;
}
bool Circle::operator<=(const Circle &c)
{
return !(r>c.r);
}
bool Circle::operator>=(const Circle &c)
{
return !(r<c.r);
}
bool Circle::operator==(const Circle &c)
{
return (c.r-r)==0;
}
bool Circle::operator!=(const Circle &c)
{
return (c.r-r)!=0;
}
int main()
{
Circle c1(3,2,4),c2(4,5,5);
cout<<"圆c1"<<c1<<"面积为"<<c1.area()<<endl;
cout<<"圆c2"<<c2<<"面积是 "<<c2.area()<<endl;
cout<<"圆c1 ";
if(c1>c2) cout<<"大于, ";
if(c1<c2) cout<<"小于, ";
if(c1>=c2) cout<<"大于等于, ";
if(c1<=c2) cout<<"小于等于, ";
if(c1==c2) cout<<"等于, ";
if(c1!=c2) cout<<"不等于, ";
cout<<"圆c2"<<endl;
return 0;
}