Point Circle Relationship---2

Source: Internet
Author: User

Enter the code:

/**copyright (c) 2015, College of Computer and Control engineering, Yantai University *all rights reserved.* file name: sum123.cpp* Author: Lin Haiyun * Completion date: June 12, 2015 * version number: v2.0** Problem Description: Overload the relational operator (6 kinds) on the circle class so that it can compare the size of two circles by the area of the circle. Self-compiled main function complete test * program input: NO * program output: Compare the size of two circles as required */#include <iostream> #include <cmath>using namespace Std;class Point{public:point (Double a=0,double b=0): X (a), Y (b) {} PROTECTED:DOUBL e x, y;};  Class Circle:public Point//circle is a common derived class of the point class {public:circle (double a=0,double b=0,double r=0): Point (A, B), radius (R) {} double area () const;    Calculate Circle Area friend Ostream &operator<< (ostream &,const Circle &);    Overloading the relational operator operator so that it can compare the size of two circles by the area of a 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 &);p rotected:double radius;};/ /Calculate Circle Area Double Circle::area () const{return 3.14159*radiUs*radius;} Overload operator "<<" to output circle information in a prescribed form ostream &operator<< (ostream &output,const circle &c) {output<    < "center=[" <<c.x<< "," <<c.y<< "], r=" <<c.radius; return output;} Overloaded relational operator (type) operator, allowing it to compare the size of two circles by the area of a circle; 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);    The area of cout<< "round C1 (" <<c1<< ") is" <<c1.area () <<endl; The area of cout<< "Round C2 (" <<c2<< ") is" <<c2.area() <<endl;   cout<< "Round C1"; if (C1&GT;C2) cout<< "Circle C1 greater than Circle C2."    <<endl; if (C1&LT;C2) cout<< "Circle C1 less than Circle C2."    <<endl; if (C1&GT;=C2) cout<< "Circle C1 is greater than or equal to round c2."    <<endl; if (C1&LT;=C2) cout<< "Circle C1 is less than or equal to round c2."    <<endl; if (C1==C2) cout<< "Circle C1 equals circle C2."    <<endl; if (C1!=C2) cout<< "Circle C1 not equal to round c2."    <<endl; return 0;}

Operation Result:


Point Circle Relationship---2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.