Class help

Ok so i have my class that im making for a game in CryEngine, and i have 2 mechs and each mech is supposed to have its own stats but i cant seem to ge tthem to change, what do i 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
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
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

class BaseMech //This class will be used for enemy and player mechs
{
    public:
        virtual void attack()
        {
            health = 5000;
            Weapon_Damage = 400;
            Ammo = 200;


            cout << health << endl;
            cout << Weapon_Damage << endl;
            cout << Ammo << endl;
        }

    private:
        size_t health; //How much health the mech has
        size_t Weapon_Damage; //How much damage each bullet does
        size_t Ammo; //How many missiles does the mech have
};


class WorldDestroyer: public BaseMech //All classes now inherit functions in ENEMY class
{
    public:
        void attack()
        {
            cout << "\n";

            health = 10000;
            Weapon_Damage = 600;
            Ammo = 40000;

            cout << health << endl;
            cout << Weapon_Damage << endl;
            cout << Ammo << endl;
        }
};


class Pulverizer: public BaseMech
{
    public:
        void attack()
        {
            cout << "\n";

            health = 6000;
            Weapon_Damage = 200;
            Ammo = 4000;

            cout << health << endl;
            cout << Weapon_Damage << endl;
            cout << Ammo << endl;
        }
};

#endif // CLASSES_H_INCLUDED 
Use the protected access specifier for members that are to be accessed from derived classes.

1
2
3
4
5
6
7
8
9
10
class BaseMech //This class will be used for enemy and player mechs
{
    
    // ... 
    
    /* private: */ protected: 
        size_t health; //How much health the mech has
        size_t Weapon_Damage; //How much damage each bullet does
        size_t Ammo; //How many missiles does the mech have
};
Ok i changed it to private and i get these errors

obj\Debug\main.o||In function `WorldDestroyer':|
virtual and inline\Classes.h|34|undefined reference to `BaseMech::BaseMech()'|
obj\Debug\main.o||In function `~WorldDestroyer':|
virtual and inline\Classes.h|34|undefined reference to `BaseMech::~BaseMech()'|
obj\Debug\main.o||In function `Pulverizer':|
virtual and inline\Classes.h|52|undefined reference to `BaseMech::BaseMech()'|
obj\Debug\main.o||In function `~Pulverizer':|
virtual and inline\Classes.h|52|undefined reference to `BaseMech::~BaseMech()'|
||=== Build finished: 4 errors, 0 warnings ===|
Never mind i dont know why but commenting out

//BaseMech();
//~BaseMech();

made it work.
You don't need the default constructor, the compiler will implicitly define it for you, but you should have:
virtual ~BaseMech() { }
in your BaseMech class
Ok this is my first attempt at polymorphism am i doing this ok? The virtual function will have something in them and the functions in each mech class will do something too.

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
#include <iostream>
#include <string>
#include "Classes.h"

using namespace std;



int main()
#include <iostream>
#include <string>
#include "Classes.h"

using namespace std;

int main()
{
    WorldDestroyer WDO;
    Pulverizer PO;
    Ravager RO;

    BaseMech *worlddestroyer = &WDO;
    BaseMech *pulverizer = &PO;
    BaseMech *ravager = &RO;

    worlddestroyer->Weap_One();
    worlddestroyer->Weap_Two();
    worlddestroyer->Weap_One_Ammo();
    worlddestroyer->Weap_Two_Ammo();
    worlddestroyer->Mech_Health();

    delete worlddestroyer;
}



Classes.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef CLASSES_H_INCLUDED
#define CLASSES_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

typedef unsigned long long Super;

class BaseMech //This class will be used for enemy and player mechs
{
    public:
        //Basic Mech functions, called when no other functions
        //exist.
        virtual int Weap_One()
        {

        }
        virtual int Weap_Two()
        {

        }
        virtual int Weap_One_Ammo()
        {

        }
        virtual int Weap_Two_Ammo()
        {

        }
        virtual int Mech_Health()
        {

        }


    protected:
        Super Health; //How much health the mech has
        size_t Weapon_One_Damage; //How much damage each bullet does
        size_t Weapon_Two_Damage;
        size_t Weapon_One_Ammo_Count; //How many missiles does the mech have
        size_t Weapon_Two_Ammo_Count;
};

//Bosses

//Boss one

class Ravager: public BaseMech
{
    public:
        int Weap_One()
        {
            Weapon_One_Damage = 600;
            cout << Weapon_One_Damage << endl;
            return Weapon_One_Damage;
        }
        int Weap_Two()
        {
            Weapon_Two_Damage = 450;
            cout << Weapon_Two_Damage << endl;
            return Weapon_Two_Damage;
        }
        int Weap_One_Ammo()
        {
            Weapon_One_Ammo_Count = 600;
            cout << Weapon_One_Ammo_Count << endl;
            return Weapon_One_Ammo_Count;
        }
        int Weap_Two_Ammo()
        {
            Weapon_Two_Ammo_Count = 700;
            cout << Weapon_Two_Ammo_Count << endl;
            return Weapon_Two_Ammo_Count;
        }
        int Mech_Health()
        {
            Health = 10000;
            cout << Health << endl;
            return Health;
        }
};


//Final Boss
class WorldDestroyer: public BaseMech //All classes now inherit functions in ENEMY class
{
    public:
        int Weap_One()
        {
            Weapon_One_Damage = 900;
            cout << Weapon_One_Damage << endl;
            return Weapon_One_Damage;
        }
        int Weap_Two()
        {
            Weapon_Two_Damage = 900;
            cout << Weapon_Two_Damage << endl;
            return Weapon_Two_Damage;
        }
        int Weap_One_Ammo()
        {
            Weapon_One_Ammo_Count = 5000;
            cout << Weapon_One_Ammo_Count << endl;
            return Weapon_One_Ammo_Count;
        }
        int Weap_Two_Ammo()
        {
            Weapon_Two_Ammo_Count = 7000;
            cout << Weapon_Two_Ammo_Count << endl;
            return Weapon_Two_Ammo_Count;
        }
        int Mech_Health()
        {
            Health = 10000000;
            cout << Health << endl;
            return Health;
        }
};


class Pulverizer: public BaseMech
{
    public:
        int Weap_One()
        {
            Weapon_One_Damage = 200;
            cout << Weapon_One_Damage;
            return Weapon_One_Damage;
        }
        int Weap_Two()
        {
            Weapon_Two_Damage = 200;
            cout << Weapon_Two_Damage;
            return Weapon_Two_Damage;
        }
        int Weap_One_Ammo()
        {
            Weapon_One_Ammo_Count = 400;
            cout << Weapon_One_Ammo_Count << endl;
            return Weapon_One_Ammo_Count;
        }
        int Weap_Two_Ammo()
        {
            Weapon_Two_Ammo_Count = 300;
            cout << Weapon_Two_Ammo_Count << endl;
            return Weapon_Two_Ammo_Count;
        }
        int Mech_Health()
        {
            Health = 4000;
            cout << Health;
            return Health;
        }
};

#endif // CLASSES_H_INCLUDED 
Last edited on
bump
that's ok but there are two problems: 1) you still don't have virtual destructor in your base class, that will cause memory leaks. 2) what's the purpose of those data members?!!! I mean why do you assign data members a constant value each call?

That's the way it's done:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class BaseMech {
    public:
        BaseMech(Super h, size_t d1, size_t d2, size_t ammo1, size_t ammo2)
              : Health(h)
              , Weapon_One_Damage(d1)
              , Weapon_Two_Damage(d2)
              , Weapon_One_Ammo_Count(ammo1)
              , Weapon_Two_Ammo_Count(ammo2)
              {}

        virtual int Weap_One() = 0; // BaseMech is an abstract base class, no definition required
        virtual int Weap_Two() = 0;
        virtual int Weap_One_Ammo() = 0;
        virtual int Weap_Two_Ammo() = 0;
        virtual int Mech_Health() = 0

        virtual ~BaseMech() { }
    protected:
        Super Health; //How much health the mech has
        size_t Weapon_One_Damage; //How much damage each bullet does
        size_t Weapon_Two_Damage;
        size_t Weapon_One_Ammo_Count; //How many missiles does the mech have
        size_t Weapon_Two_Ammo_Count;
};
Last edited on
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
class Ravager: public BaseMech
{
    public:
        explicit Ravager(Super h = 10000, size_t d1 = 600, size_t d2 = 450, size_t ammo1 = 600, size_t ammo2 = 700)
        : BaseMech(h, d1, d2, ammo1, ammo2)
        {  }

        virtual int Weap_One()
        {
            cout << Weapon_One_Damage << endl;
            return Weapon_One_Damage;
        }

        virtual int Weap_Two()
        {
            cout << Weapon_Two_Damage << endl;
            return Weapon_Two_Damage;
        }

        virtual int Weap_One_Ammo()
        {
            cout << Weapon_One_Ammo_Count << endl;
            return Weapon_One_Ammo_Count;
        }

        virtual int Weap_Two_Ammo()
        {
            cout << Weapon_Two_Ammo_Count << endl;
            return Weapon_Two_Ammo_Count;
        }

        virtual int Mech_Health()
        {
            cout << Health << endl;
            return Health;
        }

        virtual ~Ravager() { } // In case you derive another class from Ravager
};


the rest for you
Last edited on
ok i have soem questions, what are the pure virtual functions for, and why make all those functions in Ravager virtual instead of ints?
Every class with at least one pure virtual function is an abstract base class(i.e you can not make an instance of that class). Here I believe you are using BaseMech for that purpose so I made it abstract.

The virtuals in ravager are not needed, you can make them int, but what if you later decided to derive another class from ravager and access them through a "BaseMech *" or "Ravager *" ?
I have a suggestion for you, go to sfml-dev.org, see the documentation. Take a look at the Drawable class and the Sprite class, The Sprite class "is a" Drawable. You can view the code online.
Last edited on
Topic archived. No new replies allowed.