Classes

i have looks at class tutorials and dont understand a thing of it how would i put these into classes and how would i access tehm to change or edit them???

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
#include <cstdlib>
#include <iostream>
#include <windows.h>



using namespace std;

int main(int argc, char *argv[])
{
    
    /***********************************     
                 ENEMY STATS
    ***********************************/
    //Z1 (zombie easy)
    int z1attack = 5 ;
    int z1healthbegin = 10 ;
    int z1health = 10;
    int z1bowdefense = 1 ;
    int z1defense = 1 ;
    int z1magdefense = 1;
        
    //N1 (necromancer easy)
    int n1magattack = 6;
    int n1health = 25; 
    int n1defense = 3;
    int n1bowdefense = 3;
    int n1magdefense = 4;

    /***********************************
                PLAYER STATS
    ***********************************/ 
    int pattack = 6;
    int pbowattack = 7;
    int phealth = 100;
    int pdefense = 3;
    int pbowdefense = 2;
    int pmagattack = 4;
    int pmagdefense = 3;
    // INVENTORY
    int arrows = 0;
    
    // HELPING HANDS
    int fmnumber = 0;
    int anumber = 0;
    
    
    /***********************************
              FOOTMEN STATS 
    ***********************************/
    int fmattack = 3;
    int fmhealth = 5;
    int fmdefense = 2;
    int fmbowdeffense = 0;
    int fmmagdefense = 1;
    
    /***********************************
             ARCHER STATS
    ***********************************/
    int abowattack = 3;
    int ahealth = 6;
    int adefense = 1;
    int abowdefense = 2;       
    int amagdefense = 2;
}
Hi,

for example like

"zombie.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
 class zombie {


    int attack ;
    int healthbegin  ;
    int health;
    int bowdefense;
    int defense;
    int magdefense;
// Position 
public:
    int x;
    int y;
// constructor
   void zombie(int at, int he, int he2, int bowd, int def, int magd); // body has to follow in "zombie.cpp"

// whole lot of methods, like "attack" or sth., and "move", for example .... all these methods have to be 
// equiped with a "body" like the constructor .... better in "zombie.cpp"

void move(int x, int y);            // (x, y) to define new position ....
void attack(PLAYER victim);  // PLAYER would be another class, i.e. "victim" an object of class "PLAYER"
void ch_health(int h);   //..........
void ch_attack(int at); // ..........
}


The "constructor" has to fill the fields like "attack" or whatever, so it will be a function consisting mainly of assignments and stuff like that.

The other methods should do what they are called like ...

methods and the fine difference to functions is the main issue in classes and objects, i think.
Your example only uses fields.
Differences arise, because you usually use the fields of an object like the fields of a struct, which means you won't have to declare so many names like "fmattack".

Declare a class "footman" with field "attack" instead ... simillar to "zombie"-class, as above.
In your "main"-fcn, you can then declare as many footmen or zombies as you please... using f.ex.
footman f1(... all your attributes ...);



Is that any clearer to you now ?

When you have understood that, you should read about "inheritance"... that'll please you, i guess when i look at your example.

You should by the way study a tutorial about that stuff, also because i will have made a lot of syntax errors, as usual.

Greets so far



Last edited on
Thanks really helped
Topic archived. No new replies allowed.