Issue with classes

I have programmed a couple of command line adventure games before but never used classes to do so. So I thought it might be easier to use classes, but I have a funny bug with my mob generator at the moment. Every time I use say 'mob().name' to find the name of the generated mob it changes it so I have the health of one name of another and attack of another!

Here is the main.cpp:

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
39
40
#include<iostream>
#include<cstdlib>
#include<string>
#include "level.h"
#include "mob.h"
#include "player.h"

using namespace std;

int main() {
    player();
    string command;
    string mobName;
    int x = 1;
    int calc = 0;
    int mhealth;
    while (x == 1) {
        cout << "Stats" << endl << "Health: " << player().Health << endl << "Attack: " << player().Attack << endl << endl;
        level();
        cout << "You are in " << level().des;
        if (level().mob == true) {
            mob();
            mhealth = mob().health;
        }
            while (calc == 0) {
                cout << "There is a " << mobName <<endl << endl;
                cin >> command;
                cout << endl;
                if (command == "attack") {
                    mhealth = mhealth - player().Attack;
                    cout << "You hit the " << mob().name << "!";
                    if (mhealth <= 0) {
                        cout << "You have defeated him!" << endl;
                        calc++;
                    }
                }
                cout << endl << endl;
            }
    }
} 


Here is the mob header and class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>

#ifndef MOB_H
#define MOB_H


class mob
{
    public:
        mob();
        virtual ~mob();
        int random;
        std::string name;
        int health;
        int attack;
        bool drop;
    protected:
    private:
};

#endif // MOB_H 

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
#include "mob.h"
#include <cstdlib>


mob::mob()
{
    random = rand()%3;
    if(random == 0) {
        name = "Goblin";
        health = 5;
        attack = 2;
        drop = false;
    }
    else if(random == 2) {
        name = "Lizard Man";
        health = 7;
        attack = 1;
        drop = true;
    }
    else if (random == 1) {
        name = "Ogre";
        health = 10;
        attack = 4;
        drop = true;
    }
    //ctor
}

mob::~mob()
{
    //dtor
}


Here is the level header and class:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<string>

#ifndef LEVEL_H
#define LEVEL_H


class level
{
    public:
        level();
        virtual ~level();
        bool item;
        bool mob;
        int r1;
        std::string des;
    protected:
    private:
};

#endif // LEVEL_H 



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

level::level()
{
    r1 = 0;
    if (r1 == 0) {
        des = " a room. ";
        mob = true;
    }
    //ctor
}

level::~level()
{
    //dtor
}


And finally the player header and class:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef PLAYER_H
#define PLAYER_H


class player
{
    public:
        player();
        virtual ~player();
        int Health;
        int Attack;
    protected:
    private:
};

#endif // PLAYER_H 



1
2
3
4
5
6
7
8
9
10
11
12
13
#include "player.h"

player::player()
{
    Health = 20;
    Attack = 4;
    //ctor
}

player::~player()
{
    //dtor
}


Thanks for your time :)

Btw you might also be able to help with my other clrpg if you go to www.ncesoftware.co.nr
Last edited on
You can not call constructor directly...like
player();
you have to create instance of class Player.
Player player;
it will automatically call your constructor..
Than by using that instance you can access your any member..
like,
cout << "Stats" << endl << "Health: " << player.Health << endl << "Attack: " << player.Attack << endl << endl;
Similarly for other other class..
Just to add a bit more information:

Each time you call, for instance, mob(), you are creating a temporary, unnamed mob object, and calling the constructor. So, lines 11, 18 and 30 refer to 4 different player objects, lines 19, 20 and 21 refer to 3 different level objects, and lines 22, 23 and 31 refer to 3 different mob objects.
Thanks both of you, yeah I get it now.
Should health, Attack, ect. not be private?
Topic archived. No new replies allowed.