CHAPTER1-20.cpp
Upload User: fjc899
Upload Date: 2007-07-03
Package Size: 187k
Code Size: 1k
Category:

STL

Development Platform:

C/C++

  1. //文件名:CHAPTER1-20.cpp
  2. #include <iostream.h>
  3. #include <math.h>
  4. class Point
  5. {
  6. public:
  7.      Point(double xx, double yy) { x=xx; y=yy; }
  8.      void Getxy();
  9.      friend double Distance(Point &a, Point &b);
  10. private:
  11.      double x, y;
  12. };
  13. void Point::Getxy()
  14. {
  15.   cout<<"("<<x<<","<<y<<")"<<endl;
  16. }
  17. double Distance(Point &a, Point &b)
  18. {
  19.     double dx = a.x - b.x;
  20.     double dy = a.y - b.y;
  21.     return sqrt(dx*dx+dy*dy);
  22. }
  23. void main()
  24. {
  25.     Point p1(3.0, 4.0), p2(6.0, 8.0);
  26.     p1.Getxy();
  27.     p2.Getxy();
  28.     double d = Distance(p1, p2);
  29.     cout<<"Distance is "<<d<<endl;
  30. }