Recently, the C forum is discussing, point, round, and ball OOP implementation. The specific discussion is shown below URLHTTP: //www.9cbs.net/expert/topic/772/772272.xml? Temp = .4635889. I have an opinion on the views of JINFENG_WANG and other discussion. In the post of jinfeng_wang, he gave IS-A, HAS-A between the circle and the ball. And some friends give some solutions. But I think the relationship between the first, points, circles, and the ball is really IS-A, HAS-A relationship? Does these programs really reflect the relationship between the reality world, round, and the ball? If not, what is their relationship? ? What should their solutions? Let me first look at a relatively complete program Interface2d {Virtual int getX () const = 0; Virtual int get () const = 0;}; interface point3d {Virtual int getX () const = 0; Virtual Int gety () const = 0; Virtual int GETZ () const = 0;}; interface circle2d {Virtual int getX () const = 0; Virtual int get () const = 0; Virtual Int getr () const = 0;}; interface globe {Virtual INT getX () const = 0; Virtual int getz () const = 0; Virtual int GETZ () const = 0; Virtual Int getr () const = 0;
Class Globe2Circle: INT getX ();} int get} int gety ();} int get} int gety ();} int game ();} int GETR (); CONST GLOBE & G;
Point2D, Point3d class design should have no problem, it can reflect three-dimensional and two-dimensional coordinates. But let's take a look at Circle2D, what information is it given? What is the three int variables, what is their information? Perhaps the natural idea is X, Y represents, coordinates on the circle, and R represents a radius. Ok, if I give the following implementation you can say that he is a circle? Class aspect: public circle2d {public: Virtual int getX () const {return 100;} Virtual int getY () const {return -100;} Virtual Int getr () {return 1;}} If he is a circle, then this circle is strange. One point one line is a circle? (X, y) What is it? Is it a little on a circle? If this is the origin of this round? (X, y) Is the origin? If so, how do you say that on the circle? Another can I say that aspect is a semi-circular or sector? If the aspect is a half circle (100,100) as a half circle R as a radius, can it be a circular equivalent of the radius and a circular point R as a circle R? That is, do we easily add the operator = () operation to Circle2D? Maybe there are other remedies. It is best to remedy. But no matter how to remedy, if you say your class is a circle, then at least you must pass the following verification: give any point (x, y), your class can not be judged (x, y) in your class definition on. We will look at the relationship between the circles and the ball. Class Globe2Circle: INT getX ();} int get} int gety ();} int get} int gety ();} int game ();} int GETR (); CONST GLOBE & G;
What is this relationship tells us? If we don't like the definition of Circle2D, it is no problem. Then he only tells us that globe2circle is a projection that the ball is cross-sectional on it through its dot (x, y, z) and z is 0. If this is the relationship between circles and balls, it is too narrow. If it is a projection to the cross section of (0, Y, Z)? If it is a population of the projection of the crown in (x, y, 0) means? It can be said that it is a circle or it is a ball. This is the illusion of common sense to us. If we are deep in the characteristics, there is no area without the size, how can it say that it is a circle and a ball? In parsing the geometric mid point is a Cartesian, the so-called Cartesi is a tuple (x1, x2 ... xn-1), a flute of the first element of the first element is ordered. Carli is ((x1, x2 ", x). What is a circle, a collection of two-dimensional points, all the points of the point to a certain point is fixed, we call this collection For a circle, the fixed point is called the center, and the length is called a radius. We record (X-X0) ^ 2) (Y-Y0) ^ 2 = r ^ 2. What is the ball, the collection of three-dimensional points, all the distances to a certain point of order are fixed lengths. We call this point for a ball called the center, and the length is called a radius. We record (X-X0) ^ 2) (Y-Y0) ^ 2 (Z-Z0) ^ 2 = R ^ 2. In fact, the ball (circle) is essentially a series of points that meet a function. Therefore, it is necessary to describe a circle, or a ball, there must be three necessary conditions, 1. Circle (ball) equation defined domain 2. Circle (Ball) Equation 3. The value of round (ball) equation is then Discuss the ball, circle, what is the relationship? We know a ball equation (X-X0) ^ 2) (Y-Y0) ^ 2 (Z-Z0) ^ 2 = R ^ 2. Only give a variable is unable to find out that the other two variables are just an equation. It is a circular equation. The relationship between the ball and circle is that any of the values m in a certain (x, y, z) can give this ball on the plane N = m (N is X, Y, Z any variable). projection. Then, it is given a circle, which is given a defined domain (X-X1) ^ 2 (Y-Y2) ^ 2 = R2 ^ 2, which can be obtained on this defined domain. point.
Popular speaking, the point is the projection of the line in the 0-dimensional coordinates, the line is the projection of the circle in a one-dimensional coordinate, the circle is the projection of the ball on two-dimensional coordinates. If there is four-dimensional, then the ball is the four-dimensional object in 3D. Projection. That is, the point is the definition domain of the line, and the line is a circular definition domain, and the circle is the definition domain of the ball. Regardless of point, line, circle, and the ball is a point collection, to indicate that this point is round, point, and ball, you should meet an equation. So, line, circles, balls are not object but algorithm data. Of course, you can pack the data into classes, then use abstract ways to access these data, and separate algorithms give these data to constrain. Here are the implementation of my point, line, circle, and ball. #include "stdafx.h" #include
Bool Operator <(const point } Return (* this);} Bool ismeaningless () // is a meaningless value (such as negative number on the flat surface) {for (int i = 0; i IF (__ point [0] & P) { IF (__ point [0] == p .__ point [0]) Return True; Else Return False;} Point <0> & operator = (const point <0> & p) { __point [0] = p .__ point [0]; Return (* this);} bool ismeaningness () { Double MeaningLESS = POW (-1, 0.5); if (__ point [0] == MeaningLess) Return True; Else Return False;} Double & Operator [] (const INDEX) {if (INDEX> 0) {Double K = POW (-1, 0.5); return k;} else return__point [index]; Bool Operator <(Const Point <0> & P1, Const Point <0> & P2) {INT i = 0; Point <0> & P1 = (Point <0> &) P1; Point <0> & P2 = (Point < 0> &) P2; IF (P1 [i] Template } ITerAdaptor (iTeraDaptor & Ita): iterator (ita.iterator) { } Iteradaptor & Operator () {Return ITERADAPTOR ( iTerator);} {Return Iteradaptor (- Iterator);} {RETURN ITERADAPTOR (- Iterator);} {RETURAPTOR & OPERATOR = (MultiMap } Template } Aspect () {__shift = new shift;} ~ aspect () {delete __shift;} setdefinition (Vector Line () { Point <1> & operator () (Point <0> & point) {Point <1> Temp; Temp [0] = Point [0]; Return Temp; } Template Class Circle: Public CircleParam <2> // Circular {Public: Circle (CircleParam <2> & param) {(CircleParam <2>) (* this) = param;} Point <2> Operator () (Point <1> Point) {double r_sqr = __ radius * __ radius; double x_sqr = (Point [0] -__ circle_centre [0]) * (Point [0] -__ circle_centre [0]); Double Y = POW (r_sqr-x_sqr, 0.5); POINT <2> TMP; TMP [0] = POINT [0]; TMP [1] = Y; Return TMP;}}; Class Ball: Public CircleParam <3> // Ball {Public: Ball (CircleParam <3> & PARAM ) {(* This) = param;} Point <3> Operator () (Point <2> Point) {double r_sqr = __ radius * __ radius; double x_sqr = (Point [0] -__ circle_centre [0 ]) * (Point [0] -__ circle_centre [0]); Double Y_SQR = (Point [1] -__ circle_centre [1]) * (Point [1] -__ circle_centre [1]); double z = POW (r_sqr-x_sqr -y_sqr, 0.5); Point <3> TMP; TMP [0] = Point [0]; TMP [1] = Point [1]; TMP [0] = z; returnit;}}}; INT main (int Argc, char * argv []) {double z = POW (-4, 0.5); typedef point <0> p0; typedef point <1> p1; typedef point <2> p2; typedef point <3> P3; Vector Double param; aspect <1, line> line (_ldef); // Line (discrete line) Double P2 [2] = {0.0, 0.0}; CircleParam <2> CP2 (100, P2 (P2)); Double P3 [3] = {0.0, 0.0, 0.0}; CircleParam <3> CP3 (100, P3 (P2)); Aspect <2, Circle> Circle (Line, CP2); // Taking line as a circle (discrete circle) aspect <3, ball> ball (CP3) in the domain; // with Circle as a domain of the domain ( Discrete balls) RETURN 0;}