Skip to content

Commit 0aab657

Browse files
committed
Add Classes in C++
1 parent 1862076 commit 0aab657

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

HelloWorld/src/Main.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@
22

33
#define LOG(x) std::cout << x << std::endl
44

5-
void Increment(int* value) // use pointer to get the memory address of the variable passed by as parameter so we can modify it.
5+
class Player
66
{
7-
(*value)++; // dereference the value so that we can actually write to that memory instead of modifying the pointer itself
8-
}
7+
public:
8+
int x, y;
9+
int speed;
910

10-
void Increment(int& value) // pass by references, exactly the same thing as above but clearer
11-
{
12-
value++;
13-
}
11+
void Move(int xa, int ya)
12+
{
13+
x += xa * speed;
14+
y += ya * speed;
15+
}
16+
};
1417

1518
int main()
1619
{
17-
int a = 5;
18-
int& ref = a; // ref is a, Reference is just creating an allias of a to be used as if it was a.
19-
ref = 2;
20-
21-
LOG(a);
22-
23-
Increment(a); // use ampersand to get the memory address reference of that variable
24-
LOG(a);
20+
Player player;
21+
player.x = 5;
22+
player.Move(1, -1);
2523

2624
std::cin.get();
2725
}

0 commit comments

Comments
 (0)