May 6, 2021 at 12:38am May 6, 2021 at 12:38am UTC
Task=Define class Point (with member variables x and y indicating the coordinate values)
Define two Constructors of Point. Point(); and Point(double, double);
Define the print function to print the info like“The point is (5,4).”
Overload the operator + as a friend function of class Point.
Define the friend function double getDistance(Point p1, Point p2) using Euclidean distance formula.
Problem:
im struggling how to set the p3 as Ecludian distance formula , could anyone help me plz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
#include <cmath>
using namespace std;
class Point
{ private :
double x,y;
public :
Point(){
this ->x = 0;
this ->y = 0;
}
Point(double x_value, double y_value){
this ->x = x_value;
this ->y = y_value;
}
void print()
{
cout<<"The Point is " <<x<<"," <<y<<endl;
}
friend Point operator +(Point&, Point&);
friend double getDistance(Point p1, Point p2);
};
Point operator +(Point& p1, Point& p2){
Point p3;
{p3.x = (p2.x - p1.x)^2;
p3.y = (p2.y - p2.y)^2;
p3 = sqrt(p3.x - p3.y);
return p3;
}
};
int main()
{
Point p1(2,3),p2(4,5),p3;
cout<<getDistance(p1,p2);
p3=p1+p2;
cout<<p3;
return 0;
}
Last edited on May 6, 2021 at 12:38am May 6, 2021 at 12:38am UTC
May 6, 2021 at 1:30am May 6, 2021 at 1:30am UTC
^ isnt power in c++.
just say sqrt(x*x+y*y)
std::pow(base, exp) (eg pow(10,2) is 10*10) will do it too but its overkill for square
^ is xor (exclusive or, which in logic means true xor true is false, unlike regular or where true or true is true).
Last edited on May 6, 2021 at 1:32am May 6, 2021 at 1:32am UTC
May 6, 2021 at 3:19am May 6, 2021 at 3:19am UTC
Hello jonnin
first i want to thank u for ur reply
i changed the power sign , but it show less errors but still not working
May 6, 2021 at 5:19am May 6, 2021 at 5:19am UTC
You return the distance in the getDistance(...) function. Not in operator+(...) where you just add the values of x and y.
What is this on line 43?