-
Notifications
You must be signed in to change notification settings - Fork 0
/
moreOverloading.cpp
73 lines (41 loc) · 1.1 KB
/
moreOverloading.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
#include <iostream>
using namespace std;
// what does const do : it declares the "variable, method or object" constant, which means it cannot be modified
class Vector2 {
public:
Vector2(float x = 0, float y = 0) :x(x), y(y) {
}
Vector2 operator+(const Vector2& vec) const {
Vector2 v;
v.x = this->x + vec.x;
v.y = this->y + vec.y;
return v;
}
bool operator==(const Vector2 vec) const{
return (this->x == vec.x && this->y == vec.y);
}
int getX() const {
return x;
}
int getY() const {
return y;
}
//Vector2 operator==(const Vector2& vec)
private:
int x;
int y;
};
// since "cout <<" cannot ouput a Vector2, we can overload it like this and tell him how to print it.
ostream& operator<<(ostream& stream, const Vector2& vec) {
stream << vec.getX() << " " << vec.getY() << endl;
return stream;
}
int main() {
Vector2 position(5, 5);
Vector2 position2(5, 5);
Vector2 finalPosition;
finalPosition = position + position2;
cout << finalPosition << endl;
cout << "Is position and position2 the same ? " << (position == position2) << endl;
system("pause");
}