-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_overloading.cpp
More file actions
43 lines (32 loc) · 1.01 KB
/
Copy pathoperator_overloading.cpp
File metadata and controls
43 lines (32 loc) · 1.01 KB
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
#include <iostream>
struct Vector2 {
float x, y;
Vector2(float x, float y): x(x), y(y) {}
Vector2 Add(const Vector2& other) const {
return Vector2(x+other.x, y+ other.y);
}
Vector2 operator+(const Vector2& other) const {
return Add(other);
}
Vector2 Multiply(const Vector2& other) const {
return Vector2(x * other.x, y * other.y);
}
Vector2 operator*(const Vector2& other) const {
return Multiply(other);
}
};
std::ostream& operator<<(std::ostream& stream, Vector2& other) {
stream << "x: " << other.x << ", " << "y: " <<other.y;
return stream;
}
int main() {
Vector2 position(0.1f, 0.2f);
Vector2 speed(0.1f , 0.2f);
Vector2 powerup (0.5f, 1.1f);
Vector2 res = position.Add(speed.Multiply(powerup));
Vector2 res2 = position + speed * powerup;
//std::cout << res.x << res.y << std::endl;
//std::cout << res2.x << res2.y << std::endl;
std::cout << res << std::endl;
std::cout << res2 << std::endl;
}