(5)在圆类上重载关系运算符(6种),使之能够按圆的面积比较两个圆的大小。自编main函数完成测试。
代码:
#include <iostream> using namespace std; class Circle; class Point { protected: int x; int y; public: Point(int a,int b):x(a),y(b) {} friend ostream &operator <<(ostream &out,Point &a); friend int locate(Point &a,Circle &b); }; ostream &operator <<(ostream &out,Point &a) { cout<<"x="<<a.x<<" y="<<a.y<<endl; return out; } class Circle:public Point { protected: int r; public: Circle(int a,int b,int c):Point(a,b),r(c) {} friend ostream &operator <<(ostream &out,Circle &a); friend int locate(Point &a,Circle &b); bool operator>(Circle a){return this->r>a.r;} bool operator>=(Circle a){return !(this->r<a.r);} bool operator<(Circle a){return this->r<a.r;} bool operator<=(Circle a){return !(this->r>a.r);} bool operator==(Circle a){return this->r==a.r;} bool operator!=(Circle a){return !(this->r==a.r);} }; ostream &operator <<(ostream &out,Circle &a) { cout<<"圆心:"<<" ("<<a.x<<"<"<<a.y<<") "<<"半径:"<<a.r<<endl; return out; } //int locate(Point &a,Circle &b) //{ // return (b.r*b.r-(a.x-b.x)*(a.x-b.x)-(a.y-b.y)*(a.y-b.y)); //} int main( ) { Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1 cout<<"圆c1: "<<c1; cout<<"圆c2: "<<c1; cout<<"比较两个圆的面积大小。"<<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; }
运行结果:
原文地址:http://blog.csdn.net/ljd939952281/article/details/46041951