第七周C++实验报告(2)
来源:互联网 发布:知乎数据挖掘考研 编辑:程序博客网 时间:2024/06/06 20:58
- #include<iostream>
- #include<Cmath>
- using namespace std;
- class CPoint
- {
- private:
- double x;
- double y;
- public:
- CPoint(double xx=0, double yy=0):x(xx),y(yy){}
- double distance1(const CPoint &p);
- friend double distance2(const CPoint &p1,const CPoint &p2);
- double getx() const {return x;}
- double gety() const {return y;}
- };
-
- double distance3(const CPoint &p1,const CPoint &p2);
-
- double CPoint::distance1(const CPoint &p)
- {
- double dx=this->x-p.x;
- double dy=this->y-p.y;
- return sqrt(dx*dx+dy*dy);
- }
-
- double distance2(const CPoint &p1,const CPoint &p2)
- {
- double dx=p1.x-p2.x;
- double dy=p1.y-p2.y;
- return sqrt(dx*dx+dy*dy);
- }
-
- double distance3(const CPoint &p1,const CPoint &p2)
- {
- double dx=p1.getx()-p2.getx();
- double dy=p1.gety()-p2.gety();
- return sqrt(dx*dx+dy*dy);
- }
-
- int main()
- {
- CPoint p1(2,4);
- CPoint p2(3,7);
- cout<<p2.distance1(p1)<<endl;
- cout<<distance2(p1,p2)<<endl;
- cout<<distance3(p1,p2)<<endl;
- system("pause");
- return 0;
- }