Book Reviews


User Interfaces In C ++ And Object-Oriented Programming

Reviewed By David D. Clark


The author has ten years programming experience and is a senior research scientist in the advanced technology group of Miles Diagnostics, a manufacturer of medical diagnostics instruments and reagents.

Many of you may remember the early days of the 8086 when software developers were scrambling to get anything running on the new processor. In many cases, the old 8080 assembly language was simply translated to 8086 assembly language. Few of the improved 8086 features were used by the translated programs. Reading User Interfaces in C++ and Object-Oriented Programming reminded me of those days. The code in the book appears to be an almost mechanical translation of regular C into C++. The code uses very few of the useful features of C++. Although the phrase "Object-Oriented Programming" appears in the title, it is given short shrift in the book itself.

The introduction starts with a succint definition of a user interface and why it is important. Though the purpose of the book is to build a user interface toolbox in C++ for the PC and compatibles, "C" could just as easily substitute for "C++" in most of the discussions. The introduction also lists hardware and software requirements.

Chapter Contents

The first chapter of the book is devoted to the PC display. It explains the advantages and disadvantages of using MS-DOS services, BIOS services and direct manipulation of display memory to handle the low-level video aspects of a user interface. The book maintains that controlling video memory directly is the quickest way to display and store video information. In taking this approach, the programmer must prevent "snow" and "flicker" on the Color Graphics Adapter (CGA). The section on avoiding flicker by using the vertical and horizontal retrace periods is marred by an editing error: an earlier paragraph is replicated where a description of the mechanics of retrace detection should appear.

The next chapter covers using assembly language routines to access hardware and begins with a discussion of interfacing assembly language routines with C++. Short, but clear, explanations of function and variable naming, parameter passing and return values are presented with assembly language fragments as examples. The examples are based on Zortech C++ v1.0 so the specifics may differ for v2.0

The majority of the chapter consists of an assembly language listing of low-level video routines to handle saving and displaying rectangular regions of text on the screen, drawing boxes, setting attributes, setting the video mode and a single low-level routine to read characters from the keyboard.

Chapter three begins a series of chapters with listings of C/C++ code used to build a user interface. Though entitled "The C++ Input/Output Functions", the chapter contains no C++ functions. The second listing, lowlevel.cpp, contains C functions which generally call the PC-BIOS to handle cursor movement, shape, setting video attributes, etc. There are also a couple of functions used to print a character, with or without an attribute. Another function prints a string centered on a particular column. The listing is given the .cpp suffix only because it includes the windows.hpp header file, which does contain declarations of C++ objects.

Chapter four covers pointing devices, discussing the Microsoft mouse services and how to access them. Appendix C contains a more complete description of the services. A short listing implements a polling interface to the mouse driver. A second listing generalizes this interface into a C++ class called pointer. Though it would be easy to derive additional pointer devices like trackballs and digitizers, the book never uses the pointer class again. The menu functions, discussed below, use the mouse functions to access the driver directly.

Chapter five fleshes out a C++ window class. A window's private data consists of location information, cursor position within the window, an attribute, a border type (single or double), and a scroll bar type. Several other private methods do coordinate adjustments. Public methods open, close and draw the window, set the cursor position within the window or return its position, clear all or part of the window, draw horizontal and vertical scroll bars, scroll the window contents, and print text within the window.

Chapter six presents three types of menu objects: popups, pulldowns and dialogs. In my opinion, this chapter is an example of how not to create C++ objects. Though the menu objects are related and have many similarities, they are implemented as separate, unrelated classes. A more object-oriented approach might have derived these specialized menu classes from a more general, parent menu class, which might have descended from the window class.

The author also seems to implement the menu classes inconsistently. The only private data associated with the popup and dialog classes are their positions and a flag. The selection text for the menu items is stored elsewhere and only associated with the object at the time a menu must draw itself. This seems to violate good data encapsulation guidelines.

Chapter seven presents data entry in plain C. The field types supported include date, dollars, long integers, telephone numbers, Social Security numbers and general strings.

The author seems to miss another opportunity to use features of C++. He could have used the C++ class inheritance mechanism to build a hierarchy of data entry field types. The string field might have been the parent class. The long integer field could have descended from the string field. Fields for dates, dollars, Social Security and telephone numbers could have been written as descendants of the long integer field class. Such a class heirarchy would also make adding new field types easier.

The final chapter presents a ledger program that exploits some of the features of the user interface library. I am not an expert in accounting, but the program seems quite usable. The interface functions which handle windows, menus, and input/output appear to work quite well in this application.

Appendices

Slightly more than half of the book is devoted to appendices. The first is a function reference for the routines presented in chapters two through seven. It is followed by still another reference on the PC/XT/AT BIOS services. I think other sources exist that provide more and better descriptions of the BIOS services.

The third appendix references the services available from a Microsoft compatible mouse driver. I found this appendix interesting, but of little use. The descriptions were very brief, containing little explanation of why a particular service might be useful.

The final, small appendix describes how to compile the interface functions, put them in a library, and compile and link the sample ledger program. Instructions are provided for Zortech and Guidelines C++. No specific version numbers are given, but I presume the author used Zortech C++ v1.0.

Compiling And Linking The Examples

Either the Microsoft assembler, MASM, or the Borland assembler, TASM, can be used to assemble the low-level video access routines. TASM ran with no problems.

The C++ code in the book will not compile and link without errors using Zortech C++ v2.0. These problems are not the fault of the code, but are due to the differences between C++ 1.0 and C++ 2.0. Several errors are due to missing function prototypes and can be fixed simply by including the appropriate header files.

I ran into problems when trying to compile the sample ledger program. It uses the standard C library functions bsearch and qsort, which require pointers to functions as arguments. Both functions take a pointer to a function which takes two pointer arguments, compares the two things pointed at, and returns an int. Both qsort and bsearch expect a pointer to a C function. Since pointers to C functions are not type compatible with pointers to C++ functions, you must change the comparison function in the ledger program to something like:

extern "C" {
   int compare(const void *, const void *);
}
That is the portable way to declare functions with C linkage in a C++ program.

Related problems occur during linkage. You must declare the prototypes for the assembly language functions in windows.hpp as having normal C linkage. You can place an extern statement similar to the one above in the header file. Finally, the assembly language functions call two functions in mouse.cpp. The mouse functions must be declared as having standard C linkage and recompiled.

Once these changes are made, the interface library functions and example program compile and link without error. The program then runs fine.

Conclusions

Although the book purportedly deals with object-oriented programming, it only uses one facet of that programming metaphor: the use of objects themselves. The book covers none of the other features of C++, such as inheritance or polymorphism. Even though data abstraction and encapsulation are used in some objects, they are not in others. For example, one of the menu types has its text contained within the object while other objects have the text exist independently from the menu object itself.

There is nothing wrong functionally with the code presented in the book. I found only one error, in the way the right arrow key is handled by the dialog class. While the code shows good function, it does not demonstrate particularly good C++ style.

If you need user interface routines to handle simple text windows and menus, consider Mr. Goodman's other book containing routines written in C. (It's even a couple of bucks cheaper.) If you already have similar routines written in C, this book is not going to be much of an improvement. If you are interested in how you might use C++ and object-oriented programming to build a flexible user interface library, wait for something else.

User Interfaces In C ++ And Object-Oriented Programming
Mark Goodwin
MIS Press 1989
$26.95 book only,
$53.95 book and disk; 394 pages.