Create a program that uses two classes. Each class will be in a separate source
ID: 671012 • Letter: C
Question
Create a program that uses two classes. Each class will be in a separate source file with
an appropriate header file. The first class is Die. It will have a data member to indicate the
number of sides (given by user). It will have a member function that returns the value of a single roll of a single
die. The second class will be LoadedDie. It will also have a single data member that indicates the
number of sides(will match number given by user for 1st die). It will have a member function that returns the value of a single roll of a single
die. In this case it should return values that are higher than normal. You can’t just add one to the
roll. Getting a 7 on a 6-sided die stands out!
In a fifth file you will create a program that rolls each die N times. You should print the result of
each roll to the screen as well as the total of the N rolls of each die. The user must be able to enter a different number for die sides and rolls each time!
Explanation / Answer
// Die.h
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "LoadedDie.h"
int LoadedDie::roll(){
srand(time(0));
int weight = rand() % 10;
if(weight < 7) return rand() % sides + 1;
else return rand() % (sides - 1) + 1;
}
// Die.cpp
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "Die.h"
int Die::roll(){
srand(time(9));
return rand() % sides + 1;
}
// LoadedDie.h
#ifndef LOADED_DIE
#define LOADED_DIE
class LoadedDie{
public:
int sides;
int roll();
};
#endif
// LoadedDie.cpp
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "LoadedDie.h"
int LoadedDie::roll(){
srand(time(0));
int weight = rand() % 10;
if(weight < 7) return rand() % sides + 1;
else return rand() % (sides - 1) + 1;
}
// a.cpp
#include <iostream>
#include "Die.cpp"
#include "LoadedDie.cpp"
using namespace std;
int main(){
int s, n;
cout << "Enter the size of the die: ";
cin >> s;
cout << "Enter N: ";
cin >> n;
Die d;
LoadedDie l;
d.sides = s;
l.sides = s;
int dTotal = 0, lTotal = 0;
cout << "Die Loaded Die ";
int t1, t2;
for(int i = 1; i <= n; i++){
t1 = d.roll();
t2 = l.roll();
dTotal += t1;
lTotal += t2;
cout << t1 << " " << t2 << " ";
}
cout << "Die total: " << dTotal << " Loaded die total: " << lTotal << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.