mercredi 4 mars 2020

Getting same values with srand time null in Inheritance class

I am trying to revise what i have learned in class and making a simple Inheritance with "Student" class. Whenever i create a student, it creates StudentStats which is supposed to have random values in constructor as you see in the code. But i always get the same values from constructor. I don't see what is missing, i included time.h and srand(time(null)) is in the constructor.

It may be a beginner problem but i really don't see what s wrong :/

Here is the student class:

#pragma once
#include <iostream>
#include <string>
#include "Personne.h"
#include "PTUT.h"
#include "Marks.h"
#include "StudentStats.h"
#include "StudentClass.h"

using namespace std;

class PTUT;
class StudentClass;
class Etudiant : public Personne, public Marks, public StudentStats

Here is the StudentStats constructor:

#include "StudentStats.h"
#include <iomanip>
#include <time.h>

StudentStats::StudentStats()
{
    srand(time(NULL));
    Morale = (rand() % 81) + 20 ;
    MathLevel = (rand() % 81) + 20;
    ProgLevel = (rand() % 81) + 20;
    NetworkDBLevel = (rand() % 81) + 20;
    GeneralLevel = (rand() % 81) + 20;
}

And the result i get (I create 4 students in main and stats of each student is separeted with "--------"):

Results




Aucun commentaire:

Enregistrer un commentaire