Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following lists a Dice class that simulates rolling a die with a different numberof sides. The default is a standard die with six sides.

The following lists a Dice class that simulates rolling a die with a different numberof sides. The default is a standard die with six sides. The rollTwoDice functionsimulates rolling two dice objects and returns the sum of their values. The srandfunction requires including cstdlib.

class Dice{ public: Dice(); Dice( int numSides); virtual int rollDice() const; protected: int numSides; };

Dice::Dice() { numSides = 6; srand(time(NULL)); } Dice::Dice(int numSides) { this->numSides = numSides; srand(time(NULL)); } 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 ofyour choosing. Invoke the rollTwoDice function in a loop that iterates ten times and verify that the functions are working as expected.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 the function returns the largest number possible (i.e., numSides ), otherwise it returns what Dices rollDice function returns.

Test your class by replacing the Dice objects in main with LoadedDice objects.You should not need to change anything else. There should be many more dice rolls with the highest possible value. Polymorphism results in LoadedDices rollDice function to be invoked instead of Dices rollDice function inside rollTwoDice

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions