VERIFY E zyBooks offers more resources to help you NEED EXTRA PRACTI 15.22 Activ
ID: 3823991 • Letter: V
Question
VERIFY E zyBooks offers more resources to help you NEED EXTRA PRACTI 15.22 Activity Apr 25 Students This content is controlled by your instructor, and is not s content. Direct questions or concerns about this content to your If you have any technical issues with the zyLab with lab?" the bottom of the lab. Dice class int number Sides int roll Dice() o Returns a random number between 1 and numbersides o Virtual? Constructor(s) int rollTwoDice (const Dice& dice1, const Dice& dice2) return die 1. rollDice die2. rollDice() LoadedDice class, derived from Dice class Default constructor Constructor that takes one int as argument o Argument is the numbersides Override rollDice() function o 50% chance return numberSides o 50% chance same as Dice's rollDice() function int main srand (1); Dice dice (6), dice2(12); Loaded Dice loaded Die1 (6), loadedDie2(24) cout diel die rollTwoDice (diel, die2) endlExplanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
class Dice
{
private:
int numberSides;
public:
Dice(int n){numberSides = n;}
int rollDice() const
{
return rand()%numberSides + 1;
}
int getNumberOfSides()
{
return numberSides;
}
};
class LoadedDice: public Dice
{
public:
LoadedDice(int n) : Dice(n){}
int rollDice()
{
int bias = rand()%2;
if (bias == 1)
{
return getNumberOfSides();
}
else
{
return Dice::rollDice();
}
}
};
int rollTwoDice(const Dice& die1, const Dice& die2)
{
return die1.rollDice() + die2.rollDice();
}
int main()
{
srand(1);
Dice die1(6), die2(12);
LoadedDice loadedDie1(6), loadedDie2(24);
cout << "die1 + die2 = " << rollTwoDice(die1, die2) << endl;
cout << "die1 + loadedDie1 = " << rollTwoDice(die1, loadedDie1) << endl;
cout << "loadedDie1 + die2 = " << rollTwoDice(loadedDie1, die2) << endl;
cout << "loadedDie1 + loadedDie2 = " << rollTwoDice(loadedDie1, loadedDie2) << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.