fork download
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Point
  7. {
  8. private://Данные -члены(закрытые)
  9. int x,y;
  10. public: //функции -члены
  11. friend Point operator+(Point pt1,Point pt2);
  12. void set(int new_x,int new_y);
  13. int get_x();
  14. int get_y();
  15. double operator%(Point &pt);
  16. };
  17.  
  18. double Point::operator%(Point &pt)
  19. {
  20. int d1=pt.x-x;
  21. int d2=pt.y-y;
  22. return sqrt((double)(d1*d1+d2*d2));
  23. }
  24. Point operator+(Point pt1,Point pt2){
  25. Point new_pt;
  26. int a=pt1.x+pt2.x;
  27. int b=pt1.y+pt2.y;
  28. new_pt.set(a,b);
  29. return new_pt;
  30. }
  31. int main()
  32. {
  33. Point pt1,pt2;
  34. pt1.set(10,20);
  35. cout<<"pt1 is"<<pt1.get_x();
  36. cout<<","<<pt1.get_y()<<endl;
  37. pt2.set(-5,-25);
  38. cout<<"pt2 is"<<pt2.get_x();
  39. cout<<","<<pt2.get_y()<<endl;
  40. pt1.set(20,20);
  41. pt2.set(24,23);
  42. cout<<"расстояние "<<pt1%pt2<<endl;
  43.  
  44. return 0;
  45. }
  46. void Point::set(int new_x,int new_y)
  47. {
  48. if (new_x<0)
  49. new_x*=-1;
  50. if(new_y<0)
  51. new_y*=-1;
  52. x=new_x;
  53. y=new_y;
  54. }
  55.  
  56. int Point::get_x()
  57. {
  58. return x;
  59. }
  60.  
  61. int Point::get_y()
  62. {
  63. return y;
  64. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
pt1 is10,20
pt2 is5,25
расстояние 5