Lab8

CS 215 Summer 2012 Lab 08 "Inheritance, Polymorphism and Recursion "

**Goals and Objectives**
From the lecture, the class text (Ch 10) and your recent work, you should be familiar with the concept of inheritance among classes. Today we are going to explore some of the more useful ways to use inheritance in C++ when coupled with dynamic binding using pointers.

Also in class you have been studying recursion, which is often a very useful tool to have when programming. It allows you to define a particular function or procedure by referencing itself. For instance, we can check if a list is sorted by generally saying: a list is sorted if its first element is less than its second, and the list containing the second element to the last element is also sorted. In this lab we will become better acquainted with this concept.

First you will need to create a Die class (singular of dice). Let this class have a public function of int throwDie; In your implementation of the Die class, you should have this function always return 0.
 * Tasks **
 * 1) ** Create the Classes **

Now create three child classes that inherit from Die. Call these classes SixSideDie, TenSideDie , and TwentySideDie. Each of these classes should implement their own int throwDie function. Have them return a random number between 1 and the corresponding maximum (6 for the SixSideDie etc).

As you know, arrays are only allowed to hold one type of variable. However, using inheritance and pointers we can assign multiple types of child classes to one array of the parent class type. Write a main function using your classes similar to the following: int main { Die* myDice[3]; myDice[0] = new SixSideDie; myDice[1] = new TenSideDie; myDice[2] = new TwentySideDie;
 * 2. Inheritance Magic **

cout << "6-sided: " << myDice[0]->throwDie << endl; cout << "10-sided: " << myDice[1]->throwDie << endl; cout << "20-sided: " << myDice[2]->throwDie << endl; return 0; } What is output when you run this code? How can you explain this? Now go and append the word “virtual” to the beginning of your function in the Die class (so it looks like virtual int throwDie; Re-run the code. Now what is returned?

If we know for a fact that we will never be using a function in the base class since each derived class will need to have its own, it might seem a bit silly to write an implementation of the function in the base class. Look up the syntax for an “abstract class” and make the throwDie function in the base class Die abstract. What syntax needed to be changed?

You should be familiar by now with the private and public privacy levels of a class. Now create a new variable on the Die object: protected: int sides; Notice it has a privacy level of protected. Run some tests in your code to figure out what the “protected” privacy class does. (HINT: You should try accessing the variable from both the main function and one of your child classes.) How is this privacy class different from private and public.
 * 3. A new privacy level **

**For the following tasks (answer questions written in //italics//) **
4. **Interesting Tidbits and Easter Eggs** Before we start on the actual work, it is interesting to note that many open source projects as well as others have embraced the idea of recursion even in the titles of the projects themselves. For instance: Wine (Wine Is Not an Emulator) GNU ( GNU's Not Unix ) PHP (PHP: Hypertext Preprocessor) //How are these acronyms relevant to our discussion of recursion?// //Search the word “recursion” in Google. Does anything interesting happen?//

Now let's get down to business. As mentioned above, a recursive function is one that is defined by calling itself somewhere from within its own code. However, we cannot just simply write a function like: int addNumbers(int num){ return (num + addNumbers(num-1)); } While this is certainly a recursive function, it will never return. It will continue to call itself until the program crashes from running out of memory. All recursive functions need some condition under which they will halt. This condition is referred to as the “base case.” //If we wanted the function above to add together every number from 0 to num, what would the base case be?// //Rewrite the function above to include the base case so that it returns correctly.//
 * 5. But More Seriously**

Now let's say that you want to compute the factorial of a positive integer //n// using a recursive function. Remember that n factorial, written as //n!//, is defined as //n! = n*(n-1)*(n-2)* … * 2 * 1//

//Using the function above as a guide, revise it so that it computes n! for a positive integer n.//

Now let's say that you have the following function prototype: void backwardsString(char* myString, int length);

//Implement a recursive version of this function that prints out the reverse of any c-string passed in. (HINT: Recall that a c-string is just a character pointer to the first character of the c-string array.)//

Now think about this function and re-write it so that it prints out the characters of the c-string (recursively) in forward order:

void forwardsString(char* myString, int length);

//What is the relationship between the code that prints it forward and the code that prints it in reverse?// (Hint: examine where the cout statement is with respect to where the recursive call is…do you "recurse" //before// or //after// you print?)