Class Help

I'm trying to learn classes. I was originally using a struct for this.
Is there a way to incorporate my variable, "numbombs" without making it part of the object array? Or maybe a better way of going about this.

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
int numbombs = 30;
class BombClass{
    public:
        void setX(int X){
            x = X;
        }
        void setY(int Y){
            y = Y;
        }
        int getX(){
            return x;
        }
        int getY(){
            return y;
        }
        void setAvailable(bool a){
            available = a;
        }
        bool getAvailable(){
            return available;
        }
        BombClass();
    private:
        int x;
        int y;
        bool available;
};
BombClass bomb[99];
BombClass::BombClass()  //Constructor
{
    for(int i = 1; i <= numbombs; i++)
    {
        bomb[i].setX(rand() %3400 + 200); 
        bomb[i].setY(50); //rand() %200;
        bomb[i].setAvailable(true);
    }
}
Why don't you want to make it part of the object array? Is it because every object will have a copy of it?
Yes. It just doesn't seem like that's how it should be done.
Make it part of the class, but declare it as a static variable. Then, memory for only one instance will be allocated, and all the objects will share it.
The way I have the code above is the way I want it to work only with "numbombs" within the class. Another words...I wouldn't want call it with an index number.

I'd like to call it with a class method like:
 
bomb.setNumbombs(30)


Not like:
 
bomb[index].setNumbombs(30)


I think a static variable only holds it's value out of scope. I just don't want to refer to it within an array.
Check this out (and read the comments):

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

using namespace std;

class BombClass
{
public:
    // Move numbombs into the class, declaring it as static.
    static int numbombs;

    void setX(int X)
    {
        x = X;
    }

    void setY(int Y)
    {
        y = Y;
    }

    int getX()
    {
        return x;
    }

    int getY()
    {
        return y;
    }

    void setAvailable(bool a)
    {
        available = a;
    }

    bool getAvailable()
    {
        return available;
    }

    BombClass()
    {
        BombClass bomb[99];
        for(int i = 1; i <= numbombs; i++)
        {
            bomb[i].setX(rand() % 3400 + 200); 
            bomb[i].setY(50);
            bomb[i].setAvailable(true);
        }
    }
private:
    int x;
    int y;
    bool available;
};

// Initialize numbombs (this is required).
int BombClass::numbombs = 30;

int main()
{
    // You can modify numbombs whenever you want,
    // without having to initialize an object.
    BombClass::numbombs = 40;
    
    return 0;
}

Also, recall that static can be used in three different ways, one of them being what you wrote above.
I see how your doing that. I also didn't know I could initialize objects from within the constructor.
Can I initialize numbombs from within the constructor as well?

This is very helpful by the way, Thanks.
Apparently you can't initialize objects from within the constructor or the class?

I'm getting "bomb" was not declared in this scope throughout my program.
Topic archived. No new replies allowed.