[Table of Contents] [Previous] [Next] [Object Central Home]
The Essence of Object-Oriented Programming
Copyright © 1998, Bruce E. Wampler
1.1
Introduction
1.2 Object
Orientation
1.3 Object-Oriented
Languages
1.4 The Essence
of OO and C++
1.5 The Payoff
of Objects
1.6 A Case
Study
1.7 The Rest
of the Book
This book is about object-oriented software development. Object-oriented development is more than simply writing some code in C++, Java, or Eiffel, but includes the complete process - analysis of the problem, design of a solution, coding, and finally long term maintenance. Object orientation1 has the potential for huge productivity gains, but only if it is used as part of a complete process. Object orientation requires a fundamental change in the way you think about developing programs. Before you can understand and effectively use the OO development process, you must first understand objects and object-oriented design.
If you check out your local bookstore's computer book section, or read some of the latest programming journals, you might get the impression that OO means programming in C++ or Java. While this is not untrue, it is only part of the story. The rest includes Object-Oriented Analysis and Object-Oriented Design2.
While ultimately it takes an OO programming language to build a working software system, the development process really needs OOA and OOD. Developing an OO system directly in C++ without first going through the OOA and OOD steps is like building a house without first analyzing the requirements of the house, designing it, and producing a set of blueprints. You might end up with a roof over your head, but the rooms would likely be scattered all over the place, some rooms might be missing, and the whole thing would probably come tumbling down on your head during the first storm (Figure 1.1). A C++ program built without OOA and OOD might seem to work, but it is much more likely to be full of bugs, and break when you make the first modification.
Objects are the heart of object orientation. An object is a representation of almost anything you need to model in a program. An object can be a model of an employee, a representation of a sensor, a window in a user interface, a data structure such as a list, virtually anything. One way to think of an object is as a black box with some buttons and lights (Figure 1.2). This could be a TV, a car, whatever. To use the object, you need to know what the buttons do, which ones you need to press to get the object to do what you need, and what the lights mean about the status of the object. The details of how the box is put together inside are irrelevant while you are using the box. A software object is not much different. It has well-defined methods for interacting with the outside world, and can provide information about its current state. The internal representation, algorithms, and data structures are hidden from the outside world.
In the simplest terms, designing an OO system consists of identifying what objects the system contains, the behaviors of the objects, and how the objects interact with each other. One of the most neatest things about OO software development is that this simple methodology connects and integrates the analysis, design, and programming phases of the development process.
Object-oriented development is a major advance for software development. Although it may not be a magic bullet that solves all the problems associated with producing software, it is really better that older methodologies. While other development methodologies, such as structured design and programming, have many valid points, many which carry over and are used for OO development, the different phases of the development cycle have not been unified as they can be with OO. The ability to carry over the analysis to the design, and finally to the coding, can lead to big gains in productivity.
OO can produce very elegant, easy to understand designs, which leads to programs that are elegant and easy to understand. Individual objects can often be implemented and debugged independently. Libraries of existing objects can be easily reused and adapted to new designs. But what may be most important for long-term productivity gains, a good OO program is easy to modify, and resistant to the introduction of bugs during program modification and maintenance.
There are several object-oriented programming languages available to choose from, including Smalltalk, Eiffel, C++, Objective C, Objective Pascal, Java, and even Ada. There are two clear marketplace winners, C++ and Java.
For many reasons, C++ is the emerging winner as the OO language of choice for major native applications. Probably the main reason is that it was derived from C, and thus has a heritage of being able to do real things on real systems. There is also compatibility with existing C code.
Java is also increasing in popularity, mostly due to the incredible expansion of the World Wide Web. Because of the ever increasing importance of the Web, it seems likely that the number of Java applications will exceed that of C++ before too long.\
However, this book will concentrate on using C++ whenever a real language is needed as a reference for object-oriented ideas. Future versions will probably concentrate more on Java. Almost all of the principles discussed here will apply equally to C++ and Java.
There are a couple of very important points to remember about C++. It is true that you can use C++ as a ``better'' C, but this has nothing to do with objects. You should try to forget that C++ has anything at all to do with C. Developing object-oriented programs requires a totally different way of thinking. This book concentrates on the object-oriented aspects of C++.
Second, C++ has grown into a very large and complicated language. It is fairly difficult to achieve competence in the full language. Fortunately, you don't have to know or use all of C++ to write excellent object-oriented programs. This book will focus on the practical considerations of using C++ to implement objects, and not on all the syntactic sugar available with the language.
There are several different object-oriented development methodologies in use today3. Each has its strengths and weaknesses, and, unfortunately, each has its own notation. It is possible, however, to extract the essence of the different OOA and OOD methodologies, and summarize them quite concisely. This book covers OOA and OOD using a simplified version of the Coad-Yourdon methodology, which is one of most straightforward of the bunch.
Learning C++ can be a rather difficult experience. By first getting a good understanding of that basics of objects, OOA, and OOD, learning C++ becomes a much more manageable task. By approaching C++ with an understanding of objects, you will be able to use the essential core of C++ the way it was intended, to build objects. You can focus on how to use C++ to implement objects instead of being lost in the vast forest of its syntactic detail.
This is not to say that all the extra features of C++ are useless, because they are not. They can get in the way as you are first learning the language. Eventually, as you build more objects, the reason for some of the other features of C++ will become evident.
Object orientation can lead to big productivity payoffs, but not without some cost. While any kind of software development should include analysis and design phases before beginning coding, many software projects have managed to get by without them. Analysis and design are much more critical for successful OO projects.
Once you begin coding, there are other front-end costs, especially with C++. First, there is some tedious overhead code that must be implemented for every object4. Each object is also typically implemented as two separate files, so you end up with many short files to keep track of. This leads to some often boring front-end efforts that you must slog through to get to a working system.
In the end, however, the payoff is huge. First, an object-oriented design is likely to be simple and easy to understand. Once designed, you can often implement the objects separately. Once finished, each object tends to be robust and bug free. As you make changes to the system, existing objects continue to work. And as you improve existing objects, their interface to the world stays the same, so the whole system continues to work. It is this ease of change and robustness that really makes OO development different, and well worth the front-end overhead.
Having a good grasp of objects and object-oriented design is essential for understanding C++. It is also useful to have good examples to study and learn from. This web site also contains a complete case study of a substantial object-oriented C++ application. This is the implementation of V, a portable graphical user interface library. Not only is this example substantial enough to get a real idea of how OO and C++ work, it represents a real-world problem.
V is not a toy example. You might even find it useful for developing your own applications. The complete source for V\ is available at the Object Central web site. It is covered by the GNU Library Public License, so you can use the code for yourself under the terms of the license.
Eventually, this book will be organized into three parts. The first part, which is mostly complete now, covers objects. Objects and classes are defined. Then the basics of Coad-Yourdon object-oriented analysis and design are covered.
After you have read part one to get an understanding of objects, you will be ready for part two, which will cover C++ when it gets finished. After explaining the basics, it will cover in some depth how to use C++ to build objects.
The third section will be a case study of V GUI library. The evolution and design of V will be covered, followed by an examination of the C++ code. The complete source of V is available at the Object Central web site.
Visits:
1 From now on, we use OO for object-oriented.
2 OOA and OOD.
3 For example, Booch, Firesmith, Coad-Yourdon. See the bibliography for other references.
4 Constructors and destructors, for example.