I'm making a pokemon battle code. Here are codes that I've made. First, I define Pokemon class.
(pokemon.h)
class Pokemon{ ...
private:
int hp;
public:
int gethp();
void sethp(int n);
...}
(pokemon.cpp)
...
int Pokemon::gethp(){return hp;}
void Pokemon::sethp(int n){hp = n;}
...
Next, I define Trainer class. Trainer has a list of pokemons.
(trainer.h)
class Trainer: public Pokemon {
private:
Pokemon pokemons[3];
...
public:
Pokemon getpokemon(int n);
void setpokemons(int n, int m, int i);
...
(trainer.cpp)
Pokemon Pikachu(..., 160, ...) //Pikachu's hp is defined to be 160.
...
Pokemon makepokemon(int n) {
if (n == 1) { return Pikachu; }
....
}
Pokemon Trainer::getpokemon(int n) { return pokemons[n-1]; }
void Trainer::setpokemons(int n, int m, int i) {
pokemons[0] = makepokemon(n);
pokemons[1] = makepokemon(m);
pokemons[2] = makepokemon(i);
}
...
Now, when I use gethp/sethp in the main fucntion, I have a problem.
(main part)
Trainer me;
me.setpokemons(1, ...); // My first pokemon is pikachu then.
...
cout << me.getpokemon(1).gethp(); //This gives 160.
me.getpokemon(1).sethp(80); //I want to change Pikachu's hp into 80.
cout << me.getpokemon(1).gethp(); //Still this gives 160.
The problem is that sethp is not working. I guess that I need to use call by reference at some point to fix this, but I don't get how I should do.
How can I fix this problem?