77 lines
1.0 KiB
Markdown
77 lines
1.0 KiB
Markdown
---
|
|
created: 2026-04-16 08:01
|
|
course:
|
|
topic:
|
|
related:
|
|
type: lecture
|
|
status: 🔴
|
|
tags:
|
|
- university
|
|
---
|
|
## 📌 Summary
|
|
|
|
> [!abstract]
|
|
>
|
|
|
|
---
|
|
|
|
## 📝 Content
|
|
|
|
## Classes
|
|
C approach of defining a Pokemon:
|
|
```C
|
|
typedef struct {
|
|
int exp;
|
|
int level;
|
|
int attack;
|
|
int defense;
|
|
int speed;
|
|
int hp;
|
|
} Pokemon;
|
|
|
|
void upgrade(*Pokemon p, int gainedExp) {
|
|
p->exp += gainedExp;
|
|
if (p->exp >= 4000) {
|
|
p->exp -= 4000;
|
|
p->level += 1;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
Pokemon fukano = {3600, 1, 400, 400, 20, 1200};
|
|
printf("Exp of fukano: %d", fukano);
|
|
upgrade(&fukano, 400);
|
|
printf("Exp of fukano: %d", fukano);
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
In C++ we use classes:
|
|
```C++
|
|
class Pokemon {
|
|
public: // visibility modifier
|
|
int exp;
|
|
int level;
|
|
int attack;
|
|
int defense;
|
|
int speed;
|
|
int hp;
|
|
|
|
void upgrade(int gainedExp) {
|
|
exp += gainedExp;
|
|
if (exp >= 4000) {
|
|
exp -= 4000;
|
|
level += 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
Pokemon fukano = {3600, 1, 400, 400, 20, 1200};
|
|
cout << "Exp of fukano: " << fukano.exp << endl;
|
|
fukano.upgrade(400);
|
|
cout << "Exp of fukano: " << fukano.exp << endl;
|
|
}
|
|
```
|
|
|