mardi 29 novembre 2016

Generate random numbers for two different objects [duplicate]

This question already has an answer here:

How can we make a constructor generate a random number whenever we call a new object ? For example in this code :

class Box
{
public:
Box()
{ 
    number = rand() % 9;
};

int number; 
};

int main()
{
    Box b1;
    cout << b1.number;
    Box b2; 
    cout << b2.number; 
    }

I always get the same number value for b1 and b2




Aucun commentaire:

Enregistrer un commentaire