-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint2d.cpp
More file actions
47 lines (34 loc) · 900 Bytes
/
point2d.cpp
File metadata and controls
47 lines (34 loc) · 900 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
#include <iostream>
#include <cmath>
class Point2d
{
private:
double m_x {0};
double m_y {0};
public:
Point2d() = default;
Point2d(double x, double y)
: m_x{x}
, m_y{y}
{
std::cout << "Constructor called\n";
}
void print() const
{
std::cout << "Point2d(" << m_x << "," << m_y << ")\n";
}
double distanceTo(const Point2d& p)
{
return std::sqrt((m_x - p.m_x)*(m_x - p.m_x) + (m_y - p.m_y)*(m_y - p.m_y));
}
};
int main()
{
Point2d first{};
Point2d second{ 3.0, 4.0 };
// Point2d third{ 4.0 }; // should error if uncommented
first.print();
second.print();
std::cout << "Distance between two points: " << first.distanceTo(second) << '\n';
return 0;
}