c question Write a class called Point that contains two doub

c++ question:

Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get methods for both data members. It should have a constructor that takes two double parameters and uses them to initialize its data members. It should have a default constructor that initializes both coordinates to 0 (also using the set methods). It should also contain a method called distanceTo that takes a Point object as a parameter and returns the distance from that Point to the Point that we invoked the method on. You will need to use sqrt(). For example at the end of the following, dist should be equal to 5.0:

Solution

Point.cpp

#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
double x;
double y;
public:
Point(double x1, double y1){
x = x1;
y = y1;
}
Point(){
x =0;
y=0;
}
double getX(){
return x;
}
double getY(){
return y;
}
void setX(double x1){
x = x1;
}
void setY(double y1){
y = y1;
}
double distanceTo(Point p){
return sqrt((p.getX() - x) * (p.getX() - x) + (p.getY() - y) * (p.getY() - y));
}
};
int main()
{
Point p1(-1.5, 0.0);
Point p2(1.5, 4.0);
double dist = p1.distanceTo(p2);
cout<<\"Distance: \"<<dist<<endl;

return 0;
}

Output:

Distance: 5

c++ question: Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get methods for both data members. It

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site