-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path06-point2.cpp
49 lines (46 loc) · 1.01 KB
/
06-point2.cpp
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
// 06-point2.cpp : a Point type with getter and setters which check for values being within range
import std;
using namespace std;
struct Point {
void setX(int nx)
{
if (nx < 0) {
x = 0;
}
else if (nx > screenX) {
x = screenX;
}
else {
x = nx;
}
}
void setY(int ny)
{
if (ny < 0) {
y = 0;
}
else if (ny > screenY) {
y = screenY;
}
else {
y = ny;
}
}
auto getXY() const {
return pair{x, y};
}
static const int screenX{ 639 }, screenY{ 479 };
private:
int x{}, y{};
};
int main() {
cout << "Screen is " << Point::screenX + 1 << " by " << Point::screenY + 1 << '\n';
Point p;
int user_x{}, user_y{};
cout << "Please enter x and y for Point:\n";
cin >> user_x >> user_y;
p.setX(user_x);
p.setY(user_y);
auto [ px, py ] = p.getXY();
cout << "px = " << px << ", py = " << py << '\n';
}