/*SIMPLE ANALOGY:
You’re giving a locked box (Point) to someone (Line class), and
they can only open it using a key (getter function) — even though they own the box now.*/
/*so to summarize the whole process..... we first set the value of two point by taking input.
then send this two setted point to set in line class's two point.now two point in line class is also setted.
but line class dont know the value as it has to access the points through point class
altough it has point type veriable. thats when why claculation was done with line class's points
but had to use getx gety to still get the value*/
#include <iostream>
#include <cmath>
using namespace std;
// Class to represent a 2D point
class Point {
private:
double x, y;
public:
// Set the x and y values
void set(double xValue, double yValue) {
x = xValue;
y = yValue;
}
// Get x and y values
double getX() { return x; }
double getY() { return y; }
// Show the point nicely
void show() {
cout << "(" << x << ", " << y << ")";
}
};
// Class to represent a line between two points
class Line {
private:
Point start, end; // Two ends of the line
/*even if they were public we would just avoid setting the function .in main function
line.start = firstPoint;
line.end = secondPoint;it could have done but still getx and gety was needed*/
public:
// Set the two points
void setEnds(Point a, Point b) {
start = a;
end = b;
}
// Calculate length of the line
double getLength() {
double dx = start.getX() - end.getX();
double dy = start.getY() - end.getY();
return sqrt(dx * dx + dy * dy);
}
// Calculate midpoint of the line
Point getMidpoint() {
Point middle;
double midX = (start.getX() + end.getX()) / 2;
double midY = (start.getY() + end.getY()) / 2;
middle.set(midX, midY);
return middle;
}
};
int main() {
Point firstPoint, secondPoint;
double x1, y1, x2, y2;
cout << "Enter x and y for the first point: ";
cin >> x1 >> y1;
firstPoint.set(x1, y1);
cout << "Enter x and y for the second point: ";
cin >> x2 >> y2;
secondPoint.set(x2, y2);
Line line;
line.setEnds(firstPoint, secondPoint); // Tell the line what points to use
cout << "\nLength of the line: " << line.getLength() << endl;
Point mid = line.getMidpoint();
cout << "Midpoint of the line: ";
mid.show();
cout << endl;
return 0;
}