Appendix A - Additional Program Examples
Published online by Cambridge University Press: 05 June 2012
Summary
PROCEDURAL AND OBJECT-ORIENTED ORGANIZATION
This appendix uses an extended example to illustrate some of the differences between object-oriented and conventional program organization. Sections A.1.1 and A.1.2 contain two versions of a program to manipulate geometric shapes, the second with classes and objects, the first without. The object-oriented code is written in C++, the conventional code in C. To keep the examples short, the only shapes are circles and rectangles.
The non-object-oriented code uses C structs to represent geometric shapes. For each operation on shapes, there is a function that tests the type of shape passed as an argument and branches accordingly. We refer to this program as the typecase version of the geometry example, as each function is implemented by a case analysis on the types of shapes.
In the object-oriented code, each shape is represented by an object. Circle objects are implemented by the circle class, which groups circle operations with the data needed to represent a circle. Similarly, the rectangle class groups the data used to represent rectangles with code to implement operations on rectangles. When an operation is done on a shape, the correct code is invoked by dynamic lookup.
- Type
- Chapter
- Information
- Concepts in Programming Languages , pp. 509 - 520Publisher: Cambridge University PressPrint publication year: 2002