|

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 needs
to add a resistor to the device she is creating,
she doesn’t typically build a new one from scratch.
She walks over to a bin of resistors, examines the
colored bands that indicate the properties, and
picks the one she needs. The resistor is a "black
box" as far as the engineer is concerned—she doesn’t
much care how it does its work as long as it conforms
to her specifications; she doesn’t need to look
inside the box to use it in her design.
The
property of being a self-contained unit is called
encapsulation. With encapsulation, we can accomplish
data hiding. Data hiding is the highly valued characteristic
that an object can be used without the user knowing
or caring how it works internally. Just as you can
use a refrigerator without knowing how the compressor
works, you can use a well-designed object without
knowing about its internal data members.
Similarly,
when the engineer uses the resistor, she need not
know anything about the internal state of the resistor.
All the properties of the resistor are encapsulated
in the resistor object; they are not spread out
through the circuitry. It is not necessary to understand
how the resistor works in order to use it effectively.
Its data is hidden inside the resistor’s casing.
C++
supports the properties of encapsulation and data
hiding through the creation of user-defined types,
called classes. You’ll see how to create classes
in Unit 6, "Basic 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. Inheritance and
Reuse When the engineers at Acme Motors want to
build a new car, they have two choices: They can
start from scratch, or they can modify an existing
model. Perhaps their Star model is nearly perfect,
but they’d like to add a turbocharger and a six-speed
transmission. The chief engineer would prefer not
to start from the ground up, but rather to say,
"Let’s build another Star, but let’s add these additional
capabilities. We’ll call the new model a Quasar."
A Quasar is a kind of Star, but one with new features.
C++
supports the idea of reuse through inheritance.
A new type, which is an extension of an existing
type, can be declared. This new subclass is said
to derive from the existing type and is sometimes
called a derived type. The Quasar is derived from
the Star and thus inherits all its qualities, but
can add to them as needed. Inheritance and its application
in C++ are discussed in Unit 12, "Inheritance,"
and Unit 15, "Advanced Inheritance." Polymorphism
The new Quasar might respond differently than a
Star does when you press down on the accelerator.
The Quasar might engage fuel injection and a turbocharger,
while the Star would simply let gasoline into its
carburetor. A user, however, does not have to know
about these differences. He can just "floor it,"
and the right thing will happen, depending on which
car he’s driving.
C++
supports the 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 in Unit 10,
"Advanced Functions," and Unit 13, "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. It
is expected that by the end of the decade, C++ will
be the predominant language for commercial software
development.
While
it is true that C++ is a superset of C, and that
virtually any legal C program is a legal C++ program,
the leap from C to C++ is very significant. 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.
|