please need help with this program. c++ only. I don\'t need a new main, I want t
ID: 3574294 • Letter: P
Question
please need help with this program. c++ only. I don't need a new main, I want to use what I have, thanks.
PROJECT DETAILS
********************************************
the following lists a dice class that simulates rolling a die with a different number of sides. the default is a standard die with six sides. the rollTwoDice function simulates rolling two dice objects and returns the sum of their values. the srand function requires including cstdlib.
CLASS EXAMPLE
****************************************
class Dice
{
public:
Dice();
Dice(int numSides);
virtual int rollDice()const;
protected:
int numSides;
};
Dice::Dice()
{
numSides = 6;
srand(time(NULL)); //Seeds random number generator
}
Dice::Dice(int numSides)
{
this->numSides = numSides;
srand(time(NULL)); //Seeds random number generator
}
int Dice::rollDice()const
{
return (rand() % numSides) + 1;
}
//Take Two dice objects, roll them, and return the sum
int rollTwoDice(const Dice& die1, const Dice& die2)
{
return die1.rollDice() +die2.rollDice();
}
Write a main function that creates two Dice objects with a number of sides of your choosing. Invoke the rollTwoDice function in a loop that iterates ten times and verify that the functions are working as expected.
MAIN
**********************************************
int main( )
{
//Uncomment the line below for regular dice
//Dice die1(6), die2(6);
LoadedDice die1(6), die2(6);
// This would be the game; here we just simulate it rolling 10 times
for (int i = 0; i < 10; i++)
{
int total = rollTwoDice(die1, die2);
cout << total << " ";
}
cout << endl;
return 0;
}
next create your own class, LoadedDIce, that is derived from Dice. add a default constructor and a constructor that takes the number of sides as input. override the rollDice function in LoadedDice so that with a 50% chance he function returns the largest number possible (i.e., numSides), otherwise it returns what Dice's rollDice function returns.
test your class by replacing the Dice objects in main with LoadedDice objects. you should nto need to change anything else. there should be many more dice rolls with the highest possible value. polymorphism results in LoadedDice's rollDice function to be invoked instead of Dice's rollDice function inside rollTwoDice.
Explanation / Answer
Please find the required program along with its output. Please see the comments against each line to understand the step.
// Example program
#include <iostream>
#include <string>
using namespace std;
class Dice
{
public:
Dice();
Dice(int numSides);
virtual int rollDice()const;
protected:
int numSides;
};
Dice::Dice()
{
numSides = 6;
srand(time(NULL)); //Seeds random number generator
}
Dice::Dice(int numSides)
{
this->numSides = numSides;
srand(time(NULL)); //Seeds random number generator
}
int Dice::rollDice()const
{
return (rand() % numSides) + 1;
}
//Take Two dice objects, roll them, and return the sum
int rollTwoDice(const Dice& die1, const Dice& die2)
{
return die1.rollDice() +die2.rollDice();
}
class LoadedDice : public Dice {
public:
LoadedDice();
LoadedDice(int numSides);
virtual int rollDice()const;
};
LoadedDice::LoadedDice()
{
numSides = 6;
srand(time(NULL)); //Seeds random number generator
};
LoadedDice::LoadedDice(int numSides)
{
this->numSides = numSides;
srand(time(NULL)); //Seeds random number generator
}
int LoadedDice::rollDice()const
{
return ((rand() % 2) + 1)==1 ? numSides : Dice::rollDice();
}
int main( )
{
//Uncomment the line below for regular dice
//Dice die1(6), die2(6);
LoadedDice die1(6), die2(6);
// This would be the game; here we just simulate it rolling 10 times
for (int i = 0; i < 10; i++)
{
int total = rollTwoDice(die1, die2);
cout << total << " ";
}
cout << endl;
return 0;
}
---------------------------------------------
OUTPUT:
12 6 12 12 12 6 6 12 5 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.