-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest11.cpp
More file actions
64 lines (53 loc) · 951 Bytes
/
Copy pathtest11.cpp
File metadata and controls
64 lines (53 loc) · 951 Bytes
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
64
#include <iostream>
#include <cmath>
using namespace std;
class Point2
{
private:
double x;
double y;
public:
Point2(double n, double m)
{
x = n;
y = m;
}
double getx()
{
return x;
}
double gety()
{
return y;
}
};
class Line2
{
private:
Point2 p1;
Point2 p2;
public:
// 删除 const
Line2(Point2 &n, Point2 &m) : p1(n), p2(m) {}
double Length()
{
double dx = p1.getx() - p2.getx();
double dy = p1.gety() - p2.gety();
return sqrt(dx * dx + dy * dy);
}
};
int main()
{
Point2 pt1(1.0, 1.0);
Point2 pt2(3.0, 4.0);
Line2 line(pt1, pt2);
cout << line.Length() << endl;
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
// 改成正常变量,不使用临时对象
Point2 p3(x1, y1);
Point2 p4(x2, y2);
Line2 nLine(p3, p4);
cout << nLine.Length() << endl;
return 0;
}