# Answer to Question #18913 in C++ for mounisha

Question #18913

C++ program that takes the (x,y) coordinates of two points

and outputs the distance between them using constructors?

and outputs the distance between them using constructors?

Expert's answer

#include <iostream>

#include <math.h>

using namespace std;

struct Point {

double x;

double y;

Point() { //constructor 1

& x = 0;

& y = 0;

}

Point(double _x, double _y) { //constructor 2

& x = _x;

& y = _y;

}

};

int main() {

double x1, x2, y1, y2;

cout << "Enter coordinate of two points " << endl << endl;

cout << "Point 1" << endl;

cout << " x: ";

cin >> x1;

cout << " y: ";

cin >> y1;

cout << "Point 2" << endl;

cout << " x: ";

cin >> x2;

cout << " y: ";

cin >> y2;

Point p1(x1, y1), p2(x2, y2); //use constructor

double dx = p2.x - p1.x;

double dy = p2.y - p1.y;

double distance = sqrt(pow(dx, 2) + pow(dy, 2));

cout << endl << "Distance between two points is " << distance << endl;

return 0;

}

#include <math.h>

using namespace std;

struct Point {

double x;

double y;

Point() { //constructor 1

& x = 0;

& y = 0;

}

Point(double _x, double _y) { //constructor 2

& x = _x;

& y = _y;

}

};

int main() {

double x1, x2, y1, y2;

cout << "Enter coordinate of two points " << endl << endl;

cout << "Point 1" << endl;

cout << " x: ";

cin >> x1;

cout << " y: ";

cin >> y1;

cout << "Point 2" << endl;

cout << " x: ";

cin >> x2;

cout << " y: ";

cin >> y2;

Point p1(x1, y1), p2(x2, y2); //use constructor

double dx = p2.x - p1.x;

double dy = p2.y - p1.y;

double distance = sqrt(pow(dx, 2) + pow(dy, 2));

cout << endl << "Distance between two points is " << distance << endl;

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment