Hello guys,
This project requires the use of an array of pointers to aRandomNumberGenerator (i'll call it Random for short) objects. Both aDie and aCoin are derived classes of the Random class. I am to use the array to call the virtual function generate. My problem arises in my main class when I try to use the array of pointers to generate numbers for a coin and a die.
Every time I run this program it just crashes, this happened after I added the downcasting statements. I am stuck on this and I have no idea why. Is there another way of doing this without downcasting? Any help is appreciated and thanks in advanced!
#include <iostream>
#include "aDie.h"
#include "aCoin.h"
#include "aRandomNumberGenerator.h"
using namespace std;
int main()
{
aRandomNumberGenerator* array[2];
array[0] = new aCoin();
array[1] = new aDie();
aCoin* cDerivedPtr = dynamic_cast <aCoin *> (array[0]);
aDie* dDerivedPtr = dynamic_cast <aDie *> (array[1]);
int cTest[2];
int dTest[6];
int x = 0; //Variable that stores the return value of coin generate
int y = 0; //Variable that stores return value of dice generate
int s;
cout << "Please insert a seed value for both generators: ";
cin >> s;
cDerivedPtr->seed(s);
dDerivedPtr->seed(s);
for(int i = 0; i < 600; i++)
{
x = cDerivedPtr->generate();
cTest[x]++;
y = dDerivedPtr->generate();
dTest[y-1]++;
}
cout << "Heads: " << cTest[0] << "\n";
cout << "Tails: " << cTest[1] << "\n";
cout << "1: " << dTest[0] << "\n";
cout << "2: " << dTest[1] << "\n";
cout << "3: " << dTest[2] << "\n";
cout << "4: " << dTest[3] << "\n";
cout << "5: " << dTest[4] << "\n";
cout << "6: " << dTest[5] << "\n";
cin.get(); //Stops window from closing
cin.get();
return 0;
}