This is my first time working with classes and I'm a little confused. I'm making a game and I put the level up screen into a LevelUp class, it works but I'm wondering how I would spread int across classes, for example, I have a health int that adds +10 to it each time you level health up, since I have to declare health in this class again it doesn't add to the overall health for other classes. Is there a way to do this?
#include <iostream>
#include "Level.h"
usingnamespace std;
int main()
{
int health = 10;
cout << "you win!";
Level level up;
}
Now when they level up I want it to go to the level up screen so they can choose what is increased.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "Level.h"
#include <iostream>
usingnamespace std;
Level::Level()
{
cout << "choose what you want to increase";
//blah blah blah
health = health + 10;
}
I want this to go into the int health that I have in the main.cpp so it can be used in battles and have the increase they chose and what not.
This is what you want to do? Also make a public function getHealth(); to access characters private var health. Also you can add setHealth(); etc etc whatever you want to do.
Using a class for a level up doesn't make a lot of sense (at least not in this context).
Classes represent "things". string, vector, character, enemy, file, etc, etc. These are all "things" (nouns) that can be instantiated and manipulated.
Level ups aren't really a "thing", they're more of something that is being done. Your player is "levelling up". It's a verb. Verbs are typically functions, not classes.
So in this case, it makes much more sense for character or player to be the class, and LevelUp would be a member function of that class. Per Krofna's suggestion.
But couldn't I put it in a class so I wouldn't have to code the whole level up and battle system out over and over? Or is there another way I should do this?
...
int main() {
...
Player me;
Enemy mob;
me.attack(mob); //In the attack method you would modify me's exp (or something) if he won,
// and if it's above the max amount call a level up method, also in the Player class
// because "the player is leveling up."
...
}
...