Access another class' variables through a class pointer

Hey guys, this is a bit of a strange question, but is it possible to access the variables within the cStats class through a pointer of type cCharacter? If so, how would I do this? I have tried searching around but not really sure how to structure a good question to find good results so hopefully someone here can help.

Reason I'd like to do this is because I'm doing an exercise and it says:

"Create a class "Character". Enemy and Player will inherit from it.
GetHit function will be virtual, so you can override them in the children.
Put all redundant code into the parent class (ie, they both have cStats and the same functions)."

In the previous exercise it says to make a class called cStats, and now it's saying make a class called cCharacter and make the monster and player both inherit from it, but also inherit all the cStats variables. So I think it's saying keep the cStats class and somehow access those variables through the cCharacter class. Hopefully that makes sense.

This is just a part of the full code but it's all that's required. Obviously the cout statements in main wouldn't work as it would say 'class cCharacter' has no member named 'iHP'.

So yes, I could just store all of cStats' variables inside cCharacter, but I'd ideally like to be able to access cStats' variables through a cCharacter pointer.

Thanks :)


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class cCharacter{
    public:
        virtual void fGetHit(){}
        virtual int fDisplayHP(){ return 0; }
};

class cStats{
    public:
        int iHP;
        int iATK;
        int iDEF;
        int iSKIL;
};

class cMonster: public cStats, public cCharacter{
    public:
        cMonster(int a){
                iHP = a;
        }
        void fGetHit(int a, int b){
            iHP = iHP - a;
            if(b == 1)
            cout << "\nYou hit the monster for " << a << " damage!\n";
            else
            cout << "\nYou threw a fireball at the monster for " << a << " damage!\n";
        }
        int fDisplayHP(){
            return iHP;
        }
};


class cPlayer: public cStats, public cCharacter{
    public:
        cPlayer(int a){
                iHP = a;
        }
        void fGetHit(cMonster &monst){
            iHP = iHP - monst.iATK;
            cout << "\nThe monster hit you for " << monst.iATK << " damage!\n";
        }
        void fHealSelf();
        int fDisplayHP(){
            return iHP;
        }

        int fFireball(){
            return iSKIL;
        }
        string sClass;

};

int main(){

cPlayer oPlayer(100);
cMonster oMonster(100);

cCharacter* pPlayer = &oPlayer;
cCharacter* pMonster = &oMonster;

cout << pPlayer->iHP << endl;
cout << pMonster->iHP << endl;

return 0;

}
"Create a class "Character". Enemy and Player will inherit from it.
GetHit function will be virtual, so you can override them in the children.
Put all redundant code into the parent class (ie, they both have cStats and the same functions)."

[...]
In the previous exercise it says to make a class called cStats, and now it's saying make a class called cCharacter and make the monster and player both inherit from it, but also inherit all the cStats variables.


I think you're getting confused. I don't think it's saying that cPlayer and cMonster should inherit from cStats. I think it's saying that both cPlayer and cMonster should have a cStats object, and that it would be redundant to have both those classes contain cStats as a member, when you could just have cCharacter contain one, and have both those classes inherit from cCharacter.

So, cCharacter should have a cStats object as a data member. You can either make it protected, so that the derived classes can directly access it, or you can make it private, and add public accessor methods to cCharacter so that the derived class can assess the stats.

Edit: Remember that inheritance models an "is-a" relationship. A player is a type of character, and a monster is a type of character. However, a player isn't a type of stats - a player has stats.
Last edited on
Thanks a lot MikeyBoy. To be honest I've never come across a class having another class' object as a member before. Can't really find any specific tutorials on it either, only on general inheritance :(

This has worked for me, is it what you were explaining?:

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
41
42
43
44
45
46
47

class cStats{
    public:
        int iHP;
        int iATK;
        int iDEF;
        int iSKIL;
};

class cCharacter{
    public:
        virtual void fGetHit(){}
        virtual int fDisplayHP(){ return 34; }
        cStats oStats;
};

class cPlayer: public cStats, public cCharacter{
    public:
        cPlayer(){
                oStats.iHP = 100;
        }
        void fGetHit(int c){
            oStats.iHP = oStats.iHP - c;
            cout << "\nThe monster hit you for " << c << " damage!\n";
            oStats.iHP = oStats.iHP + oStats.iDEF;
            cout << "\nYou blocked " << oStats.iDEF << " damage!\n";
        }
        void fHealSelf();
        int fDisplayHP(){
            return oStats.iHP;
        }

        int fFireball(){
            return oStats.iSKIL;
        }
        string sClass;

};

int main(){

cPlayer oPlayer;

cout << oPlayer.oStats.iATK << endl;

return 0;
}


Thanks a lot MikeyBoy.

You're welcome.
To be honest I've never come across a class having another class' object as a member before.

Really? Because you were already doing it in the original version of your cPlayer class. You had:
string sClass;
as a data member of cPlayer, so you your cPlayer already had another class as a data member.

Can't really find any specific tutorials on it either, only on general inheritance :(

Then you really need a better textbook. Although there's nothing fundamentally difficult about it - a class is a type, like int or float, so there's nothing strange about the idea of having one as a data member.

This has worked for me, is it what you were explaining?:

Well, you've started along the right track, but you still have cPlayer inheriting from cStats. The whole point was the cPlayer shouldn't inherit from cStats, as I said in my previous post. It's not an "is-a" relationship.

Also, I'm wondering why you're making all of your data members public? That breaks the principles of encapsulation that underly OO.
Ah yeah completely forgot about inheriting cStats! Fixed it now :)

Yeah I'm quite lazy with the access specifiers at the moment, I just put everything to public so I can get a better grasp of how classes and inheritance works. So I need to read up more about them. But yeah, I'm glad I understood what you taught me. I'll go learn some more about access specifiers and then report back with some sikk OOP !

Thanks again friend :)
Last edited on
You're welcome! Glad I could help :)
Topic archived. No new replies allowed.