logo CodeStepByStep logo


Related Links:

You have been asked to extend an existing class named Dice representing a set of 6-sided dice that can be rolled by a player.

Member Description
private: int* diceValues an array of all dice values rolled
private: int count length of diceValues array
Dice(int count) constructs a dice roller to roll the given # of dice; all dice initially have the value of 6
virtual int getCount() const returns the number of dice managed by this dice roller, as passed to the constructor
virtual int getValue(int index) const returns the die value (1-6) at the given 0-based index
virtual void roll(int index) rolls the given die to give it a new random value from 1-6
virtual int total() const returns the sum of all current dice values in this dice roller
virtual string toString() const returns string of dice values, e.g. "{4, 1, 6, 5}"
ostream& operator <<(ostream& out, Dice& dice) prints the given dice in its toString format

Define a new class called RiggedDice that extends Dice through inheritance. Your class represents dice that let a player "cheat" by ensuring that every die always rolls a value that is greater than or equal to a given minimum value. You should provide the same member functions as the superclass, as well as the following new public behavior:

Member Description
RiggedDice(int count, int min) constructs a rigged dice roller to roll the given # of dice, using the given minimum value for all future rolls; all dice initially have the value 6 (if the minimum value is not between 1-6, throw an integer exception)
virtual int getMin() const returns minimum roll value as passed to constructor

For all inherited behavior, RiggedDice should behave like a Dice object except for the following differences. You may need to override or replace existing behavior in order to implement these changes.

  • Every time a die is rolled, you must ensure that the value rolled is greater than or equal to the minimum value passed to your constructor. Do this by re-rolling the die if the value is too small, as many times as necessary.
  • The rigged dice should return a total that lies and claims to be 1 higher than the actual total. For example, if the sum of the values on the dice add up to 13, your rigged dice object's total returned should be 14.
  • When a rigged dice object's toString is called or when it is printed, it should display that the dice are rigged, then the dice values, then the minimum dice value, in exactly the following format: "rigged {4, 3, 6, 5} min 2"

Also make RiggedDice objects comparable to each other by defining a < operator. RiggedDice are compared by total dice value in ascending order, breaking ties by minimum roll value in ascending order. In other words, a RiggedDice with a lower total dice value is considered to be "less than" one with a higher total. If two objects have the same total, the one with a lower min value passed to its constructor is "less than" one with a higher min value. If the two objects have the same total and the same min, they are considered to be "equal." Though a proper implementation would define other operators such as <=, >, >=, ==, and !=, we ask you to define just this one operator.

Write the .h and .cpp parts of the class both in the provided text box. You should utilize the behavior you have inherited from the superclass and not re-implement behavior that already works properly in the superclass. You may assume that the superclass already checks all arguments passed to its constructor and members to make sure that they are valid.

Inheritance: Write a C++ class using inheritance.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.