5-4-2

//在weight.h中
#ifndef _WEIGHT_H_
#define _WEIGHT_H_
class Point
{
public:
    Point(int x=0,int y=0):x(x),y(y){}
    int getX(){return x;}
    int getY(){return y;}
    friend float dist(Point &p1,Point &p2)
    {
    double x=p1.x-p2.x;
    double y=p1.y-p2.y;
    return static_cast<float>(sqrt(x*x+y*y));
    }

private:
    int x,y;
};
#endif
 1 //在main.cpp中
 2 #include<iostream>
 3 #include<cmath>
 4 #include"weight.h"
 5 
 6 float dist(Point &p1,Point &p2)
 7 {
 8     double x=p1.x-p2.x;
 9     double y=p1.y-p2.y;
10     return static_cast<float>(sqrt(x*x+y*y));
11 }
12 
13 int main()
14 {
15     Point myp1(1,1),myp2(4,5);
16 
17     std::cout<<"the distance is: ";
18     std::cout<<dist(myp1,myp2)<<std::endl;
19     return 0;
20 }

 

你可能感兴趣的:(5-4-2)