I haven't written any code yet, but would this be the way to do it(I am trying to get used to classes so let me know if there is a better/easier way to do what I am trying to do)? I am just trying to make the users monster fight against an enemy.
1) Make a class for the user's character, make a class for all the enemies, make a class the the different attacks (so three total classes).
2)The parameters of the user's character class would be attack and health, but I would pass these by reference so that they can change when attacked (right?).
3) Make an object for the enemy (use the same pass by reference here).
4) Fight them by calling different objects from the Attacks class.
My questions are: Say I wanted to use an attack, called slash. How can I use it multiple times? Do I need to make an object at the beginning of main and just keep using that?
As a rule of thumb (and especially for a beginner), a class should be created when it makes it easier to think about how to solve the problem, and especially if you're going to need more than one of them.
User character - easy to think of as an object, will have characteristics that should logically live inside that object. Only one of them.
Enemies - easy to think of as an object, will have characteristics that should logically live inside that object, lots of them.
Attacks - not at all easy to think of as an object. Attacking is something the player and enemies do to each other. What would an attack object look like? If you had such a thing, you'd have to have the player and the enemy interacting with this third object; that's making things more complicated. It doesn't make thinking about the problem easy - it makes it harder. Attacking is something that involves the player and the enemies; surely it should be a property of those classes.
This is what objects are for. To make it easier for you to think about the problem, and easier to code a solution. I see a lot of beginners get obsessed with objects and end up jamming everything they can into one (especially when they need a function; they end up making a big ball of mud to hold all the functions out of some misguided sense of elegance).
1) I would make one base enemy class, then for each different type of enemy, it would inherit stuff from the base class. Like class Enemy would have private variables such as health, attack, defense... Then class Charizard :: Enemy would have all those variables plus a fireBonus variable, or whatever. http://www.cplusplus.com/doc/tutorial/inheritance/
2) Don't pass by reference. Instead have a public function in the class (this would be in your base enemy class) called updateHealth(int). Like so:
void updateHealth(constint num) {health += num;}
3) "Making an object" is just creating one instance of a class. Like...
Charizard firstPokemon;
is creating an object of type Charizard.
4) Idk if you really need an Attacks class, rather than just Attack functions.
I figure main would look something like...
main
prompt user for user's monster's type and stats
create user's monster and enemy monsters
while (neither enemy is dead)
prompt user for which attack to use
perform both attacks i.e. slash(userPokemon, enemyPokemon);
whirlwind(enemyPokemon, userPokemon);
while end
main end
Of course the attack functions would use updateHealth(int num).
Let me know what you think. Practice using classes before tackling something too big.
I haven't read into inheritance yet, but it is coming up. I realized this was bigger than I was ready for, so I am scaling down a bit before I try something like this.
You should really learn interfaces or virtual classes and inheritance well before you go into a project like this, for example.
You your pokemons got 4 attacks but they all behave different. This could be done easy with virtual classes. The same thing goes for enemies and characters etc.
For example.
if you call charizard to do attack1 he is going to do it different from another pokemons attack1. But you could call the attack1 function from the same interface.
Sorry if this is unclear. Hope it makes some sense, just reply or PM if you want more help
Almost forgot. This article is very worth reading for you. http://cplusplus.com/articles/G13hAqkS/
But i would suggest using SDL instead. But thats just personal choice