- #ifndef POINT_H
- #define POINT_H
- extern int number;
- class Point
- {
- public:
- bool operator==(const Point &point);
- Point();
- Point(double x,double y);
- double getxval()const;
- double getyval()const;
- void setxval(const double x);
- void setyval(const double y);
- ~Point();
- private:
- double xval,yval;
- };
- #endif // POINT_H
- #include "point.h"
- #include <iostream>
- int number = 0;
- Point::Point():xval(0.0),yval(0.0)
- {
- }
- Point::Point(double x, double y):xval(x),yval(y)
- {
- number++;
- }
- double Point::getxval() const
- {
- return xval;
- }
- double Point::getyval() const
- {
- return yval;
- }
- void Point::setxval(const double x)
- {
- xval = x;
- }
- void Point::setyval(const double y)
- {
- yval = y;
- }
- Point::~Point()
- {
- }
- bool Point::operator==(const Point &point)
- {
- return (this->xval ==point.getxval())&&(this->yval==point.getyval());
- }
- #ifndef LINE_H
- #define LINE_H
- extern int number;
- class Point;
- class Line
- {
- public:
- Line();
- Line(double x,double y,double x_1,double y_2);
- double Instance();
- int getnumber();
- ~Line();
- private:
- Point *point_1;
- Point *point_2;
- };
- #endif // LINE_H
- #include "line.h"
- #include "point.h"
- #include <math.h>
- Line::Line():point_1(0),point_2(0)
- {
- }
- Line::Line(double x, double y, double x_1, double y_2):point_1(new Point(x,y)),point_2(new Point(x_1,y_2))
- {
- }
- double Line::Instance()
- {
- if(*point_1 == *point_2)
- return 0;
- double dis_x = point_1->getxval() - point_2->getxval();
- double dis_y = point_1->getyval() - point_2->getyval();
- return sqrt(dis_x*dis_x+dis_y*dis_y);
- }
- int Line::getnumber()
- {
- return number;
- }
- Line::~Line()
- {
- delete point_1;
- delete point_2;
- }
- #include <iostream>
- #include "line.h"
- #include "point.h"
- using namespace std;
- int main()
- {
- Line line(2.0,3.0,1.0,2.0);
- Line line1(2.0,3.0,1.0,2.0);
- double mydis = line.Instance();
- cout << "LINE length is "<<mydis<< endl;
- cout<<"TOTAL "<<line.getnumber()<<" PONITS"<< endl;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0308201513296.html
来源: http://www.codesnippet.cn/detail/0308201513296.html