< previous page page_159 next page >

Page 159
Convert Dimensions to Feet                    Level 1
Set heightInFeet = heightInInches / 12
Set diameterInFeet = diameterInInches / 12

Compute Surface Area
Set surfaceArea = pi * radius * sqrt(radius*radius +
  heightInFeet
*heightInFeet)

Compute Costs
Set redCost = surfaceArea * 0.10
Set blueCost = surfaceArea 
* 0.15
Set greenCost = surfaceArea 
* 0.18

Print Results
Print surfaceArea
Print redCost
Print blueCost
Print greenCost

If you look at the C++ program for ConePaint, you can see that it closely resembles this solution. The main difference is that the one concrete step at level 0 has been inserted at the proper point among the other concrete steps. You can also see that the names of the modules have been paraphrased as comments in the code.
The type of implementation that we've introduced here is called flat or inline implementation. We are flattening the two-dimensional, hierarchical structure of the solution by writing all of the steps as one long sequence. This kind of implementation is adequate when a solution is short and has only a few levels of abstraction. And the programs it produces are clear and easy to understand, assuming appropriate comments and good style.
Longer programs, with more levels of abstraction, are difficult to work with as flat implementations. In Chapter 7, you'll see that it is preferable to implement a hierarchical solution by using a hierarchical implementation. There we implement many of the modules by writing them as separate C++ functions, and the abstract steps in the design are replaced with calls to those functions.

 
< previous page page_159 next page >