第12周-多继承、虚基类,继承和组合-项目4-点、圆的关系(2)

/* 
* Copyright (c) 2014, 烟台大学计算机学院 
* All rights reserved. 
* 文件名称:test.cpp 
* 作    者:刘畅
* 完成日期:2015 年 5  月  26 日 
* 版 本 号:v1.0 
* 
* 问题描述:在圆类上重载关系运算符(6种),使之能够按圆的面积比较两个圆的大小
* 输入描述:;
* 程序输出:输出圆的中心、半径,圆与圆的大小关系:


代码如下:

#include <iostream>
#include<Cmath>
using namespace std;
#define pi 3.1415926
class Point
{
public:
    Point(double a=0,double b=0):x(a),y(b) {}
protected:
    double x,y;
};

class Circle:public 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 pi*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);
    cout<<"圆c1( "<<c1<<" )的面积是 "<<c1.area()<<endl;
    cout<<"圆c2( "<<c2<<" )的面积是 "<<c2.area()<<endl;
    if(c1>c2)
        cout<<"圆c1大于圆c2."<<endl;
    if(c1<c2)
        cout<<"圆c1小于圆c2."<<endl;
    if(c1>=c2)
        cout<<"圆c1大于等于圆c2."<<endl;
    if(c1<=c2)
        cout<<"圆c1小于等于圆c2."<<endl;
    if(c1==c2)
        cout<<"圆c1等于圆c2."<<endl;
    if(c1!=c2)
        cout<<"圆c1不等于圆c2."<<endl;
    return 0;
}


运行结果:


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值