2

Interface

class FireSimulator
{
    Landscape **forest;
    Weather w;
    static int statistics;
    const int x;
    const int y;

public:
    FireSimulator();
    FireSimulator(Weather&, int, int);

   void setBoundary();
   void Print();
};

Implementation

FireSimulator::FireSimulator() : w(),x(0),y(0)
{}

FireSimulator::FireSimulator(Weather& W, int X, int Y): w(W), x(X),y(Y)
{
    forest=new Landscape *[x];
    for(int i=0; i<x; i++)
    {
        forest[i]=new Landscape[y];
    }
}

void FireSimulator::setBoundary()
{}

void FireSimulator::Print()
{}

int FireSimulator::statistics=0;

This sets the forest of type landscape dynamically.

How do I use randomization to spread the trees around the forest?

How do I randomly initialize x and y in the constructor, which are constant data members?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Firdaws
  • 93
  • 1
  • 10

1 Answers1

2

If you have a function randomInt() returning a random integer, you could just initialize the constant members to its return value:

FireSimulator::FireSimulator()
     : w()
     , x(randomInt())
     , y(randomInt())
{}

For non-cryptographic randomness, the C standard library rand() function might be good enough for your case if well seeded with srand(). These two functions are available from #include <cstdlib>. In C++11 also provides its own randomness generation methods in <random> (see this question).

jotik
  • 17,044
  • 13
  • 58
  • 123
  • So I can just do it in the "get" function and call it in the member initialize list? – Firdaws Apr 10 '16 at 06:23
  • @Firdaws I'm sorry I don't understand your question. – jotik Apr 10 '16 at 06:25
  • Thanks, it worked !! What I meant was, can I implement the randomization in the getter function of x and then just call it in the constructor? But, it's the same thing, so thanks :) – Firdaws Apr 10 '16 at 06:29
  • I don't think it would be a good practice for a getter like `getX()` to set `x` to a random value every time it is called. – jotik Apr 10 '16 at 06:38
  • So, in the setter then? Done :) – Firdaws Apr 10 '16 at 06:57