Class Question

Oct 5, 2011 at 7:27pm
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?
Oct 5, 2011 at 9:06pm
closed account (10oTURfi)
Uhh its really hard without looking at the code...
Oct 6, 2011 at 8:47pm
Are you looking for public and/or static variables? Maybe try references and/or pointers...
Oct 6, 2011 at 10:25pm
^^
Oct 7, 2011 at 2:14am
I'll give a simple example of what I want to do.

I have my main.cpp with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "Level.h"

using namespace 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>

using namespace 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.
Oct 7, 2011 at 7:06am
closed account (10oTURfi)
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.

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
#include <iostream>
#include <string>
using namespace std;

class character
{
public:
    character(){lvl = 1; health = 50;}
    void levelchange(string a);
private:
    int lvl;
    int health;
};

void character::levelchange(string a)
{
    if(a=="up")
    { 
        cout << "Level up! Choose what you want to increase?" << endl;
        //blah blah
        lvl++;
        health += 10;
    }
    else if(a=="down")
    { 
        cout << "You lost a level!" << endl;
        --lvl;
        health -=10;
    }
}

int main()
{
    character* pTest = new (character);

    pTest->levelchange("up");
    pTest->levelchange("down");
}
Last edited on Oct 7, 2011 at 7:09am
Oct 7, 2011 at 4:48pm
Would this be how to do it if you have a separate class file?
Oct 7, 2011 at 5:14pm
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.
Last edited on Oct 7, 2011 at 5:15pm
Oct 7, 2011 at 5:17pm
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?
Last edited on Oct 7, 2011 at 5:18pm
Oct 7, 2011 at 6:24pm
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?


Why would you have to code it over and over? Just put it in a function and call the function.

Whenever the player levels up, just call the LevelUp function. You don't need to make a class for it.

It's like I said before. Classes are things/nouns, and functions are actions/verbs. Levelling up is an action. Therefore it should be a function.
Oct 7, 2011 at 6:59pm
Oh okay, thanks. I guess I had the wrong idea about classes.
Oct 7, 2011 at 11:16pm
You want to write code like this:
1
2
3
4
5
6
7
8
9
10
11
...
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."
   ...
}
...
Topic archived. No new replies allowed.