Please help, I have to send it today to teacher

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
^ 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
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
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?
Consider:

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
#include <iostream>
#include <cmath>

using namespace std;

class Point {
private:
	double x {}, y {};

public:
	Point() {}

	Point(double x_value, double y_value) : x(x_value), y(y_value) {}

	friend std::ostream& operator<<(std::ostream& os, const Point& p)
	{
		return os << "The Point is (" << p.x << ',' << p.y << ')';
	}

	friend Point operator+(const Point&, const Point&);
	friend double getDistance(const Point& p1, const Point& p2);
};

Point operator+(const Point& p1, const Point& p2) {
	return Point {p1.x + p2.x, p1.y + p2.y};
}

double getDistance(const Point& p1, const Point& p2)
{
	return std::sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}

int main()
{
	const Point p1 {2, 3}, p2 {4, 5};

	cout << getDistance(p1, p2) << '\n';

	const auto p3 {p1 + p2};

	std::cout << p3 << '\n';
}


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
56
57
58
59
60
61
62
63
#include <iostream>
#include <cmath>

using namespace std;

class Point
{
private:
    double x{0},y {0};

public:

    Point(){};

    Point(double x_value, double y_value)
    {
        x = x_value;
        y = y_value;
    }

    void print()
    {
        cout<<"The Point is " << x << ',' << y << '\n';
    }
    friend Point operator+(Point&, Point&);
    friend Point operator-(Point&, Point&);
    double getDistance(Point p2);
};

double Point::getDistance(Point p2)
{
    Point p3 = *this - p2;
    return sqrt(p3.x * p3.x + p3.y * p3.y);
}


Point operator+(Point& p1, Point& p2)
{
    Point p3;
    p3.x = p2.x + p1.x;
    p3.y = p2.y + p1.y;
    return p3;
}

Point operator-(Point& p1, Point& p2)
{
    Point p3;
    p3.x = p2.x - p1.x;
    p3.y = p2.y - p1.y;
    return p3;
}

int main()
{
    Point p1(2,3), p2(4,5) ,p3;
    cout << p1.getDistance(p2) << '\n';
    cout << p2.getDistance(p1) << '\n';

    p3 = p1 + p2;
    p3.print();

    return 0;
}


2.82843
2.82843
The Point is 6,8
Program ended with exit code: 0
Topic archived. No new replies allowed.