Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Core C++ -- A Software Engineering Approach


March 2001/Core C++ -- A Software Engineering Approach


Title:Core C++ — A Software Engineering Approach
Author:Victor Shtern
Publisher:Prentice Hall PTR, 2000
Pages:1,237 pages, soft cover
ISBN:0-13-085729-7
Price:$49.99

Introduction

The main theme of Core C++ is the engineering of code that is both easily maintainable and reusable. In this book communication between the designer of the code and the maintainence programmer is considered significant; it is important that the designer’s knowledge is transferred to the maintainer.

This book addresses experienced programmers in languages other than C++ as well as novices learning C++ as a first programming language. If you know C++ reasonably well, but feel that maintaining your programs is harder than it should be, this book is for you, too.

One of the book’s pluses is Shtern’s step-by-step technique: he avoids "forwarding references" that would allow him to use constructs discussed later in the book. This is significant, since C++ is a complex language and not easy for a beginner to learn. Shtern’s approach makes life easier for the novice, allowing the reader to take one piece of the cake at a time and not have to deal with the whole cake at once. To give further guidance and orientation the book provides special hints variously entitled "notes," "alerts," and "tips." These hints help a reader figure out what’s really important among the vast amounts of information to remember, especially for programmers new to C++.

Shtern shows a considerable number of examples — and he doesn’t hesitate to present bad code, either. That is valuable, because you can only deal with "bad code" if you know what bad code is. Having presented the bad, Shtern then discusses better solutions. The rationale is that you’ll remember "good solutions" more easily if you know why they are preferable to other ways of accomplishing the same thing.

What’s in the Book

The book is divided into four parts:

1) Introduction to programming with C++

2) Object-oriented programming with C++

3) Object-oriented programming with Aggregation and Inheritance

4) Advanced Uses of C++

Part 1 introduces both the problems encountered when dealing with large programs and the basic C++ features. The chapter about the "software crisis" deals with known problems in software development: cost overruns, late or cancelled projects, incomplete system functionality, and software errors. At this point it becomes clear what the book really is: an introduction to software engineering with C++, with a strong focus on the development of large programs — that is, programs large enough to make development by a team a necessity, not an option. Therefore the book also discusses less technical topics, such as the communication between team members, as well as how information flows from the program designer/developer to the maintenance programmer.

The next chapters introduce the basic parts of the language used in nearly every C++ program: preprocessor directives, comments, statements, functions, loop constructs, arrays, and header files, to name a few. The last chapter in this part discusses memory management and file I/O. Since no material presented in later chapters is used earlier, the book’s explanation of operator new here is a bit misleading, albeit correct for older compilers: the explanation states that new returns a null pointer if there’s not enough memory available. A standard conforming compiler would throw a bad_alloc exception. In the (hopefully rare) case that a novice encounters a bad_alloc in one of the example programs, a hint on things to come might be useful. A reference to Chapter 18 would do. There the author gives the proper description of how new behaves upon memory allocation failure. Although I agree with the author that the whole truth is too much for a beginner at times, I feel that the description given and the code presented should be in agreement with one another. In this case, using new(std::nothrow) instead of new would accomplish the desired behavior.

The book devotes considerable focus to examples. These are presented as full-fledged compilable programs in the form of "listings." For example, Chapter 2 alone presents 15 complete programs. Additionally, there are lots of "code snippets" to think about in the text. Note that there are no exercises to be found in the book. Any serious reader will be busy experimenting with changes to the given code, thus there is no lack of exercise.

Object-oriented programming is the main theme of the second part. The starting point is a chapter on functions as a modularization tool. A special focus here is on the use of const. This seems appropriate, as "const correctness" is not just a practice for the overly conscientious; it is something all programmers should strive for at all times. In my opinion Chapter 8 is one of the most important, as it explains the central ideas of software engineering in more detail: cohesion, coupling, data encapsulation, and information hiding, the latter two being distinguished from one another. Let me briefly discuss this distinction Shtern introduces.

The book states that data encapsulation is more about readability and independence of program components than about protection from unauthorized changes. For example, consider a class with accessors and mutators for all its data elements (which are private, of course), but no other member functions. Data is encapsulated, but by calling an accessor function it is still possible to change data in erroneous ways. The lesson is that data encapsulation does not necessarily protect against erroneous data changes. In this sense encapsulation does not help much (apart from allowing access to data elements without knowing their names). Furthermore, the information is not hidden. If a more complex operation is necessary, it is the client code that must accomplish the task. If we had a member function for this task, we would have hidden the information on how to accomplish it. Thus, according to Core C++, information hiding is accomplished if the application domain is separated from the data design. In that case the client code states what is done, the server code defines how it is done.

This chapter contains something else that other books often hide away from the novices eye: useless, if not bad, code — to make sure a beginner will be able to tell good style from bad. In the following chapters classes and operator functions are discussed as well as "special" member functions such as constructors and their complementary functions, destructors.

The third part shows how to combine and extend existing classes, in other words, how class aggregation and inheritance work. You’ll also find information on data members with special properties, such as constant and reference members. Chapter 12 shows how to create a class that holds a reference to an object of its own type as a data member [1]. This is one of the examples of detailed description not often found in most programming texts.

This chapter also emphasizes that there’s more to object-oriented programming than using inheritance and polymorphism. In fact, at times class aggregation is the more appropriate way to express a relationship between classes; though it may not be obvious, aggregating classes is undoubtedly an object-oriented approach. This part closes with a chapter on code reuse and a brief introduction to UML. The UML part is just a very brief introduction but nonetheless useful: you’ll understand basic UML diagrams when you encounter them later. And you’re pretty likely to do so sooner or later, if you’re following the software engineering path.

The last part is entitled "Advanced uses of C++." Virtual functions are presented as well as templates, exceptions, and multiple inheritance. The chapter on advanced operator overloading gives information on I/O operators and how to use them with user-defined classes. The introduction to exceptions in this part of the book introduces only basic information about them.

Compared to the rest of the book, this part leaves some points to be desired: whereas other chapters are generous with do’s and don’ts, this one keeps a bit quiet. A paragraph or two on exception safe and exception neutral code would come in handy. I could well imagine an "alert box" stating that an exception must never ever be allowed to leave a destructor [2]. The last and final chapter summarizes "What We Have Learned" — which turns out to be quite a lot. The book closes with a comparison of C++ and other languages: COBOL, PL/1, "Virtual Basic" [3], C — and of course, Java.

Conclusion

In summary, the book introduces readers to software engineering as well as C++. Reading it is a pleasure. Core C++ comprehensively covers the discussed material and also presents material that is not always explained in books intended for beginners.

The informal style makes reading Shtern’s book quite a bit of fun. But note that I am not claiming it is an "easy reader," as the covered material is complex at times. And working through this material is the price to pay for a thorough understanding of C++. Core C++ is worthwhile reading for beginning, intermediate, and advanced programmers. If I had to recommend a single book to students learning C++, it would certainly be Core C++.

Notes

[1] The book also recommends not using this feature unless you absolutely have to.

[2] Herb Sutter. Exceptional C++ (Addison-Wesley, 2000). Lecture 16 (in the German translation).

[3] I really like that typo!

Stephan Kämper started programming some 17 years ago, using C++ intensively since 1997. He studied physics in Bonn and received a diploma in physics from the University of Osnabrck. He has worked in the fields of holography and oceanography and will be working on software quality assurance in the future. He can be reached at Stephan.Kaemper@t-online.de.


Related Reading


More Insights