By implementing the concepts of classes and objects, write a C++ program that determines the distance between 2 points in a coordinate system. The coordinates of the 2 points: P1 (x1, y1) and P2 (x2, y2) are to be entered from the keyboard. The distance, d, between the 2 points is computed as: d = √(y2 - y1)^2 + (x2 - x1)^2
#include<iostream>
#include<cmath>
using namespace std;
class Distance{
private:
int x1, y1, x2,y2;
public:
void setX1(int i ){
x1 = i;
}
void setY1(int m ){
y1 = m;
}
void setX2(int d ){
x2 = d;
}
void setY2(int k ){
y2 = k;
}
int getX1(){
return x1;
}
int getY1(){
return y1;
}
int getX2(){
return x2;
}
int getY2(){
return y2;
}
double calculate_distance(){
int y = pow(getY2() - getY1(),2);
int x =pow(getX2() - getX1(),2);
int m = y + x;
double answer =sqrt(m);
return answer;
}
};
int main(){
cout<<"Enter the coordinates of two points to calculate the distance between them"<<endl;
int x1, x2, y1, y2;
cout<<"Enter X1"<<endl;
cin>>x1;
cout<<"Enter Y1"<<endl;
cin>>y1;
cout<<"Enter X2"<<endl;
cin>>x2;
cout<<"Enter Y2"<<endl;
cin>>y2;
Distance d;
d.setX1(x1);
d.setX2(x2);
d.setY1(y1);
d.setY2(y2);
cout<<"The distance between ("<<x1<<" , "<<y1<<") and ("<<x2<<" , "<<y2<<") is "<<d.calculate_distance()<<endl;
}
Comments
Leave a comment