2.8 Mixed Data
What do you get, when
you mix a circle and square?
virtual class equals zero
2.8.1 Objectives
what is meant by mixed data
write a data definition for mixed data with a C++ pure virtual class
communicate to C++ that a compound data class belongs to a mixed data collection with inheritance
write polymorphic functions over mixed data
choose a set of good tests for a function over mixed data.
2.8.2 Exercises (due 2011/10/07 08:45:00)
1. Develop the function perimeter, which consumes either a circle or a square and computes the perimeter.
spiders, whose relevant attributes are the number of remaining legs (we assume that spiders can lose legs in accidents) and the space they need in case of transport;
elephants, whose only attributes are the space they need in case of transport;
monkeys, whose attributes are intelligence and space needed for transportation.
3. Develop the function fitsHuh. The function consumes a zoo animal and the volume of a cage. It determines whether the cage is large enough for the animal.
4. Add rectangles to our area program from class. For our purposes, the description of a rectangle includes its upper-left corner, its width, and its height.
5. Extend the zoo animal data definition so that animals endorse products, which are compound data described by a product name and a product company. Write a function called endorse that consumes an animal and a product and produces an animal that endorses that product.
6. Develop the function biggest, which consumes two zoo animals and returns the one that takes up more space. (Hint: This requires a new function in the interface that IS NOT biggest.)
2.8.3 Optional Exercises (due 2011/10/12 08:45:00)
7. Develop the function center, which consumes either a circle or a square and computes the center point
8. Add ellipses to our area program from class. (Use Google/Wikipedia to discover what defines an ellipses.)
9. Develop the function accident, which consumes a zoo animal and returns a new animal that has been in an accident. Spiders lose legs in accidents, monkeys lose intelligence, and elephants shrink slightly.
2.8.4 Notes
These notes are primarily for my sake, but I don’t see any reason to hide them from you.
start with Posn |
|
Square (nw, length) |
Circle (center, radius) |
|
Find perimeter of each |
|
Define the perimeter function... that takes both |
- data definition |
- communicate to C++ (virtual ... = 0) |
- try |
- add to classes |
- glory |
|
Sometimes we know we want to write 'perimeter' first, so we discover our needs top-down |
|
Make sure to write print() |
|
Write translate (dx, dy) |