Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. /**
  7. * A point in 2D space.
  8. */
  9. class Point2D {
  10. public:
  11. int x, y;
  12. Point2D::Point2D (int, int);
  13.  
  14. /**
  15. * Calculates and returns the distance between two points (the current Point2D
  16. * object and a given parameter p).
  17. */
  18. double dist2D(Point2D p) {
  19. return sqrt( pow((p.x - this->x), 2) + pow((p.y - this->y), 2) );
  20. }
  21.  
  22. /**
  23. * Prints the 2D distance between two points as '2D distance = k' where k is
  24. * distance d as a ceiling-rounded int on a new line.
  25. */
  26. void printDistance(double d) {
  27. cout << "2D distance = \n" << ceil(d);
  28. }
  29. };
  30.  
  31. /**
  32. * A point in 3D space.
  33. */
  34. class Point3D: public Point2D {
  35. public:
  36. int z;
  37. Point3D(int, int, int);
  38.  
  39. /**
  40. * Calculates and returns the distance between two points (the current Point3D
  41. * object and a given parameter p).
  42. */
  43. double dist3D(Point3D p) {
  44. return sqrt( pow((p.x - this->x), 2) + pow((p.y - this->y), 2) + pow((p.z - this->z), 2) );
  45. }
  46.  
  47. /**
  48. * Prints the 3D distance between two points as '2D distance = k' where k is
  49. * distance d as a ceiling-rounded int on a new line.
  50. */
  51. void printDistance(double d) {
  52. cout << "3D distance = \n" << ceil(d);
  53. }
  54. };
  55.  
  56. int main() {
  57. int x1;
  58. int y1;
  59. int z1;
  60. int x2;
  61. int y2;
  62. int z2;
  63.  
  64. cin >> x1 >> y1 >> z1;
  65. cin >> x2 >> y2 >> z2;
  66.  
  67. Point3D p1(x1, y1, z1);
  68. Point3D p2(x2, y2, z2);
  69. double d2 = p1.dist2D(p2);
  70. double d3 = p1.dist3D(p2);
  71. //The code below uses runtime polymorphism to call the overridden printDistance method:
  72. Point2D p(0, 0);
  73. p.printDistance(d2);
  74. p = p1;
  75. p1.printDistance(d3);
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement