Wouldn't it be nice if we could access Vector elements using the same syntax we use for arrays? Well, we can, using user-defined overloaded operators:
Wouldn't it be nice if we had a uniform interface for iterating over all collection types? Well, we can, using the idea of iterators:
Note: to really make this work you need inheritance, but never mind that for now.
Wouldn't it be nice if we didn't have to define a new Vector class for every different element type? Well (seeing a theme here?) we can, using templates. We may have time to discuss templates later in the quarter, but in the meantime just keep in mind that most collections would actually be written using templates.
A recursive definition is one that uses itself. At first, this seems like a confusing idea, but we use recursive ideas all the time in everyday life:
- The balance on a savings account after 10 years is the balance after 9 years, plus the interest for the 10th year---or, in pseudocode:
balance(10) = let x = balance(9) in x + (interest_rate * x); - Where do chickens come from? (Eggs.) Where do eggs come from? (Chickens.)
The second example demonstrates the fact that there must be a base case to any recursion. That is, at some point, an animal that was not, biologically, a chicken, must have laid a chicken egg. Otherwise, we have infinite recursion.