#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {} double getx() { return x; } double gety() { return y; } friend ostream&operator << (ostream&,Point&); protected: double x,y; }; ostream&operator << (ostream &output,Point &p) { output<<"("<<p.getx()<<","<<p.gety()<<")"<<endl; return output; } class Circle:public Point { public: Circle(double a,double b,double c):Point(a,b),r(c) {} friend ostream&operator << (ostream&,Circle&); bool operator > (Circle &c); bool operator < (Circle &c); bool operator >= (Circle &c); bool operator <= (Circle &c); bool operator == (Circle &c); bool operator != (Circle &c); protected: double r; }; ostream&operator << (ostream &output,Circle &c) { output<<"("<<c.getx()<<","<<c.gety()<<")"<<" "; output<<"半径:"<<c.r<<endl; return output; } bool Circle::operator < (Circle &c) { if(r<c.r) return true; else return false; } bool Circle::operator > (Circle &c) { if(r>c.r) return true; else return false; } bool Circle::operator >= (Circle &c) { if(*this<c)return false; return true; } bool Circle::operator <= (Circle &c) { if(*this>c)return false; return true; } bool Circle::operator == (Circle &c) { if(*this>=c&&*this<=c)return true; return false; } bool Circle::operator != (Circle &c) { if(*this==c)return false; return true; } int main( ) { Circle c1(3,2,4),c2(4,5,5); cout<<"c1:"<<c1<<endl; cout<<"c2:"<<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; if (c1 <= c2) cout << "c1≤c2" << endl; return 0; }
原文地址:http://blog.csdn.net/zjx211314/article/details/26393733