Previous Table of Contents Next


C++ and Object-Oriented Programming

C++ fully supports object-oriented programming, including the four pillars of object-oriented development: encapsulation, data hiding, inheritance, and polymorphism.

Encapsulation and Data Hiding

When an engineer is creating a new device, he wires together component pieces. He can wire in a resistor, a capacitor, and a transistor. The transistor has certain properties and can accomplish certain behaviors. He can use the transistor without understanding the details of how it works, as long as he knows what it does.

To achieve this, the transistor must be self-contained. It must do one well-defined thing, and it must do it completely. Doing one thing completely is called encapsulation.

All the properties of the transistor are encapsulated in the transistor object; they are not spread out through the circuitry. It is not necessary to understand how the transistor works in order to use it effectively.

C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. Once created, a well-defined class acts as a fully encapsulated entity; it is used as a whole unit. The actual inner workings of the class should be hidden; users of a well-defined class do not need to know how the class works, they just need to know how to use it. You’ll see how to create classes in Hour 6, “Basic Classes.”

Inheritance and Reuse

In the late 1980s, I worked for Citibank building a device for home banking. We didn’t want to start from scratch; we wanted to get something out into the market quickly. Therefore, we started with the telephone and “enhanced” it. Our new enhanced telephone was a kind of telephone; it just had added features. Thus, I was able to reuse all the calling features of a plain old telephone but add new capabilities to extend its utility.

C++ supports the idea of reuse through inheritance. A new type can be declared that is an extension of an existing type. This new subclass is said to derive from the existing type and is sometimes called a derived type. The enhanced telephone is derived from a plain old telephone and thus inherits all its qualities, but additional features can be added to it as needed. Inheritance and its application in C++ are discussed in Hour 16, “Inheritance.”

Polymorphism

The enhanced telephone (ET) behaves differently when receiving a call. Rather than ringing a bell, the screen lights up and a voice says, “You have a call.” The phone company doesn’t know this, however. They don’t send special signals to each kind of phone. The company just pulses the wire and regular telephone ring, electronic telephone trill, and ET speaks. Each phone does “the right thing” based on its understanding of the message from the phone company.

C++ supports this idea that different objects do “the right thing,” through what is called function polymorphism and class polymorphism. Poly means many, and morph means form. Polymorphism refers to the same name taking many forms, and is discussed during Hour 17, “Polymorphism,” and Hour 18, “Advanced Polymorphism.”

How C++ Evolved

As object-oriented analysis, design, and programming began to catch on, Bjarne Stroustrup took the most popular language for commercial software development, C, and extended it to provide the features needed to facilitate object-oriented programming. He created C++; and, in less than a decade, it has gone from being used by only a handful of developers at AT&T to being the programming language of choice for an estimated one million developers worldwide.

C++ Isn’t Just a Better C

It is true that C++ is a superset of C and that virtually any legal C program is a legal C++ program, but don’t let that fool you. H.L. Mencken once said that Wagner’s music is “better than it sounds.” Well, the leap from C to C++ is bigger than it looks.

C++ benefited from its relationship to C for many years, as C programmers could ease into their use of C++. To really get the full benefit of C++, however, many programmers found they had to unlearn much of what they knew and learn a whole new way of conceptualizing and solving programming problems.

Should I Learn C First?

The question inevitably arises: because C++ is a superset of C, should you learn C first? Stroustrup and most other C++ programmers agree: not only is it unnecessary to learn C first, it is a bad idea. This book assumes you are not a C programmer. If you are a C programmer, however, no problem. Read the first few hours for review and then hang on tight. You’re not in Kansas any more.

Preparing to Program

C++, perhaps more than other languages, demands that the programmer design the program before writing it. Trivial problems, such as the ones discussed in the first few chapters of this book, don’t require much design. Complex problems, however, such as the ones professional programmers are challenged with every day, do require design. The more thorough the design, the more likely it is that the program will solve the problems it is designed to solve, on time and on budget. A good design also makes for a program that is relatively bug free and easy to maintain. It has been estimated that fully 90 percent of the cost of software is the combined cost of debugging and maintenance. To the extent that good design can reduce those costs, it can have a significant impact on the bottom-line cost of the project.

The first question you need to ask when preparing to design any program is, “What is the problem I’m trying to solve?” Every program should have a clear, well-articulated goal; and you’ll find that even the simplest programs in this book have one.

The second question every good programmer asks is, “Can this be accomplished without resorting to writing custom software?” Reusing an old program, using pen and paper, or buying software off the shelf are often better solutions to a problem than writing something new. The programmer who can offer these alternatives will never suffer from lack of work; finding less expensive solutions to today’s problems will always generate new opportunities later.

Assuming you understand the problem and it requires writing a new program, you are ready to begin your design.


Previous Table of Contents Next