CreateCreate a point class which can specify a point in a two dimensional plane. It should have a no-
arg constructor, which initialize the co-ordinates by 0 and also have a two-arg constructor,
which initialize the co-ordinates by the appropriate two parameters. It should also have a show(
) function, which will display the values of the two co-ordinates. Now find the distance between
two objects of the point class and show its use in the main( ) function.
[The distance between (x1,y1) to (x2,y2) points is d = √((x2-x1)²+(y2-y1)²) ]
#include <iostream>
#include <cmath>
using namespace std;
class point {
private:
double x, y;
public:
point() {
x = 0;
y = 0;
}
point(double _x, double _y) {
x = _x;
y = _y;
}
void show() {
cout << "Point: (" << x << ", " << y << ")\n";
}
double distance(point other) {
double d = sqrt(pow(other.x - x, 2) + pow(other.y - y, 2));
return d;
}
};
int main()
{
point a;
point b(3, 4);
a.show();
b.show();
cout << "Distance: " << a.distance(b) << endl;
}
Comments
Leave a comment