Expert C Programming

(Jeff_L) #1

Object-oriented programming is characterized by inheritance and dynamic binding. C++ supports
inheritance through class derivation. Dynamic binding is provided by virtual class functions. Virtual
functions provide a method of encapsulating the implementation details of an inheritance hierarchy.


Well, duh! Here we'll make a lightning tour of C++, and describe only the highlights. We'll try to
bring the framework of the language into sharp relief by leaving out many less important details. Our
approach is to look at the key concepts of OOP, and summarize the C++ features that support each.
The concepts build on one another in the logical order in which they appear here. Some of the
programming examples deliberately relate to everyday actions like squeezing juice from an orange.
Juice-squeezing is not usually achieved by software. We call functions to do it here, to focus attention
on the abstraction rather than the lowest-level implementation details. First, let's summarize the
terminology and describe it in terms of concepts we already know from C (see Table 11-1).


C++ was known by the name "C with classes" up until about 1985, but it now includes much, much
more than this. It was quite a reasonable extension to C at that point, easy to explain, implement, and
teach. Then it got caught up in a wave of enthusiasm that has not yet crested, and a lot of other
features (including the metaphorical kitchen sink) were added. To halt this, it has been suggested that
C++ should have "conservation of featurism": new features in C++ should be subject to growth
curtailment rules, like those that apply to pub licenses in the Republic of Ireland—anyone proposing
an additional one must surrender two existing ones to be withdrawn from use. You want multiple
inheritance? Sure—but you have to give up exceptions and templates!


Table 11-1. The Key Concepts of Object-Oriented Programming
Term Definition
Abstraction The process of refining away the unimportant details of an object, so that only the
essential characteristics that describe it remain. Abstraction is a design activity. The
other concepts are the OOP features that provide it.
Class A user-defined type, just as int is a built-in type. The built-in types have well-defined
operations (arithmetic etc.) on them, and the class mechanism must allow the
programmer to specify operations on the class types he or she defines, too. Anything in
a class is known as a member of the class.

Member functions of a class (the operations) are also known as methods.
Object A specific variable of a class type, just as j may be a specific variable of type int. An
object is also known as an instance of a class.
Encapsulation Grouping together the types, data, and functions that make up a class. In C, a header
file provides a very weak example of encapsulation. It is a feeble example because it is
a purely lexical convention, and the compiler knows nothing about the header file as a
semantic unit.
Inheritance This is the big one—allowing one class to receive the data structures and functions
described in a simpler base class. The derived class gets the operations and data of the
base class, and can specialize or customize them as needed. It can also add new data
and function members. There's no example in C that suggests the concept of
inheritance. C does not have anything resembling this feature.

Now C++ is a rather large language. As a concrete example, the size of a C compiler front-end might
be around 40,000 lines; a C++ compiler front-end might be twice as big, or more.

Free download pdf