-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-CLASSESvsSTRUCTSinCpp.cpp
53 lines (42 loc) · 1.19 KB
/
11-CLASSESvsSTRUCTSinCpp.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
CLASSES vs STRUCTS in C++
The only difference is that a CLASS is PRIVATE by default, while STRUCT is PUBLIC by default
Ex: WORK
struct Player {
int x, y;
int speed;
void Move( int xa, int ya) [M]
{
x += xa * player.speed; [M]
y += ya * player.speed; [M]
}
};
int main()
{
Player player;
player.Move(player, 1, -1); [M]
}
Ex: NOT WORK
struct Player {
private: [MOD]
int x, y;
int speed;
void Move( int xa, int ya) [M]
{
x += xa * player.speed; [M]
y += ya * player.speed; [M]
}
};
int main()
{
Player player;
player.Move(player, 1, -1); [M]
}
The C code doesn't have classes but it has structures. This is because in C++ exist classes and structs
if we write
#define struct class
the code will replace struct with class
How practically use STRUCT and CLASSES
STRUCT when you just want ot group variables and you don't want to add much more other functionality
In STRUCT you can also include methods
For inheritance: never use inheritance with struct. If I have some kind of inheritance Hierarchy is better to use CLASSES for that because it complexify all.
If you have inheritance from a class in a struct some compilers will give you a warning be cause using struct and class together.