-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunique.cpp
80 lines (66 loc) · 1.37 KB
/
unique.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Unique pointers
* Tanner Babcock
* tababcock@dmacc.edu
* November 6, 2021 */
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
const int NUM_POINTS = 5;
const double SCALE = 0.5;
class Point {
private:
double x, y;
public:
Point(double x, double y): x(x), y(y) { }
Point(): Point(0, 0) { }
double getX(void) { return x; }
double getY(void) { return y; }
void setX(double x) { this->x = x; }
void setY(double y) { this->y = y; }
};
unique_ptr<Point> inputPoint(void) {
double x, y;
cout << "Enter X: ";
cin >> x;
cout << "Enter Y: ";
cin >> y;
return make_unique<Point>(x, y);
}
unique_ptr<Point> scalePoint(unique_ptr<Point> p, double sc) {
p->setX(p->getX() * sc);
p->setY(p->getY() * sc);
return p;
}
vector<unique_ptr<Point>> getScaledPoints(double sc, int pts) {
vector<unique_ptr<Point>> points;
for (int i = 0; i < pts; i++) {
points.push_back(scalePoint(inputPoint(), sc));
}
return points;
}
int main(void) {
vector<unique_ptr<Point>> pointsPtr = getScaledPoints(SCALE, NUM_POINTS);
for (int i = 0; i < NUM_POINTS; i++) {
cout << "X: " << pointsPtr[i]->getX() << ", Y: " << pointsPtr[i]->getY();
cout << endl;
}
return 0;
}
/* Output:
Enter X: 2
Enter Y: 3
Enter X: 4
Enter Y: 5
Enter X: 6
Enter Y: 7
Enter X: 8
Enter Y: 9
Enter X: 10
Enter Y: 11
X: 1, Y: 1.5
X: 2, Y: 2.5
X: 3, Y: 3.5
X: 4, Y: 4.5
X: 5, Y: 5.5
*/