I am using OOP to create a basic class and I can't figure how to randomly generate a number for the type of Weapon when each Weapon is instantiated in the main Class.
//Weapon.h
class Weapon {
int type;
public:
Weapon();
~Weapon();
int getType();
};
///////////////////////////////////////////////////////////////////////////////
//Weapon.cpp
#include "Weapon.h"
#include <cstdlib>
#include <time.h>
Weapon::Weapon()
{
srand(time(NULL));
type = rand() % 4;
}
Weapon::~Weapon()
{
}
int Weapon::getType() {
return type;
}
///////////////////////////////////////////////////////////////////////////////
//main.cpp
#include <iostream>
#include "Weapon.h"
int main() {
Weapon w1, w2, w3;
std::cout << "w1 is type #" << w1.getType() << "\n";
std::cout << "w2 is type #" << w2.getType() << "\n";
std::cout << "w3 is type #" << w3.getType() << "\n";
return 0;
}
The results I am getting are: "w1 is type #1" "w2 is type #1" "w3 is type #0"
Everytime I run the program in Visual Studio, the same numbers appear and they aren't being randomized everytime the program runs. How would I achieve this? I seem to have forgotten the basics of c++ because this seems easy to me but I can't figure it out.
EDIT: Using srand in Weapon.cpp, I now get random generation on each run. But all the weapons have the same type value. How would each instantiated weapon have a different type value?