fork download
  1. /*SIMPLE ANALOGY:
  2. You’re giving a locked box (Point) to someone (Line class), and
  3. they can only open it using a key (getter function) — even though they own the box now.*/
  4.  
  5.  
  6. /*so to summarize the whole process..... we first set the value of two point by taking input.
  7. then send this two setted point to set in line class's two point.now two point in line class is also setted.
  8. but line class dont know the value as it has to access the points through point class
  9. altough it has point type veriable. thats when why claculation was done with line class's points
  10. but had to use getx gety to still get the value*/
  11.  
  12.  
  13.  
  14.  
  15. #include <iostream>
  16. #include <cmath>
  17. using namespace std;
  18.  
  19. // Class to represent a 2D point
  20. class Point {
  21. private:
  22. double x, y;
  23.  
  24. public:
  25. // Set the x and y values
  26. void set(double xValue, double yValue) {
  27. x = xValue;
  28. y = yValue;
  29. }
  30.  
  31. // Get x and y values
  32. double getX() { return x; }
  33. double getY() { return y; }
  34.  
  35. // Show the point nicely
  36. void show() {
  37. cout << "(" << x << ", " << y << ")";
  38. }
  39. };
  40.  
  41. // Class to represent a line between two points
  42. class Line {
  43. private:
  44. Point start, end; // Two ends of the line
  45. /*even if they were public we would just avoid setting the function .in main function
  46.   line.start = firstPoint;
  47.   line.end = secondPoint;it could have done but still getx and gety was needed*/
  48.  
  49. public:
  50. // Set the two points
  51. void setEnds(Point a, Point b) {
  52. start = a;
  53. end = b;
  54. }
  55.  
  56. // Calculate length of the line
  57. double getLength() {
  58. double dx = start.getX() - end.getX();
  59. double dy = start.getY() - end.getY();
  60. return sqrt(dx * dx + dy * dy);
  61. }
  62.  
  63. // Calculate midpoint of the line
  64. Point getMidpoint() {
  65. Point middle;
  66. double midX = (start.getX() + end.getX()) / 2;
  67. double midY = (start.getY() + end.getY()) / 2;
  68. middle.set(midX, midY);
  69. return middle;
  70. }
  71. };
  72.  
  73. int main() {
  74. Point firstPoint, secondPoint;
  75. double x1, y1, x2, y2;
  76.  
  77. cout << "Enter x and y for the first point: ";
  78. cin >> x1 >> y1;
  79. firstPoint.set(x1, y1);
  80.  
  81. cout << "Enter x and y for the second point: ";
  82. cin >> x2 >> y2;
  83. secondPoint.set(x2, y2);
  84.  
  85. Line line;
  86. line.setEnds(firstPoint, secondPoint); // Tell the line what points to use
  87.  
  88. cout << "\nLength of the line: " << line.getLength() << endl;
  89.  
  90. Point mid = line.getMidpoint();
  91. cout << "Midpoint of the line: ";
  92. mid.show();
  93. cout << endl;
  94.  
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter x and y for the first point: Enter x and y for the second point: 
Length of the line: 0
Midpoint of the line: (2.3388e-310, 5.81517e-310)