/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:cxz
*完成日期:2014 年 5 月 27 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述:无
*问题描述:
*问题分析://
*算法设计:
*/
#include <iostream>
#include<Cmath>
using namespace std;
class Point
{
public:
Point(double a=0,double b=0):x(a),y(b) {} //构造函数
protected: //受保护成员
double x,y;
};
class Circle:public Point //circle是Point类的公用派生类
{
public:
Circle(double a=0,double b=0,double r=0): Point(a,b),radius(r) { }//构造函数
double area ( ) const; //计算圆面积
friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
//重载关系运算符运算符,使之能够按圆的面积比较两个圆的大小;
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 radius;
};
//计算圆面积
double Circle::area( ) const
{
return 3.14159*radius*radius;
}
//重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator<<(ostream &output,const Circle &c)
{
output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius;
return output;
}
//重载关系运算符(种)运算符,使之能够按圆的面积比较两个圆的大小;
bool Circle::operator>(const Circle &c)
{
return (this->radius - c.radius) > 1e-7;
}
bool Circle::operator<(const Circle &c)
{
return (c.radius - this->radius) > 1e-7;
}
bool Circle::operator>=(const Circle &c)
{
return !(*this < c);
}
bool Circle::operator<=(const Circle &c)
{
return !(*this > c);
}
bool Circle::operator==(const Circle &c)
{
return abs(this->radius - c.radius) < 1e-7;
}
bool Circle::operator!=(const Circle &c)
{
return abs(this->radius - c.radius) > 1e-7;
}
int main( )
{
Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1
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;
}
成果展示: