I have a class "Horse" with a speed member (1-100). I want the default constructor to generate a random number for the speed, but when I dynamically allocate memory for an array of type Horse, each horse has the same speed.
Here is how I allocated the memory within main:
horsePtr = new Horse[numHorses];
Here is the constructor
Horse::Horse() {
srand(time(0));
maxDistancePerSecond = (rand() % 100) + 1;
distanceTraveled = 0;
racesWon = 0; }
I know it's a mistake to include srand within the constructor, but the class file won't let me put it anywhere else. How can I get around this?
p.s. I have to use arrays and the srand function (no vectors) :)
Aucun commentaire:
Enregistrer un commentaire