That is, the internal functions roll and value are the only functions that are allowed to access its value. This prevents a rogue programmer from assigning an invalid value to the die. It is the job of the function value to return the value of currentValue so that other parts of the program can view it. The roll function is currently the only entity besides the constructor that changes the value of currentValue . It uses the rand function found in stdlib.h to assign a value in the proper range.
Inheritance Suppose a cavalier programmer decides to invent a multi-sided die.
It would also require the changing of the definition of roll . One problem with this method is that all the code that uses the current definition of PlainDie would have to be rewritten. Also, it seems a shame to have the overhead of generality when almost all the dice will have 6 sides anyway. Inheritance to the rescue. This example demonstrates the use of inheritance to produce an enhanced class called Die .