PDF 14feb24 0858 Splitted
PDF 14feb24 0858 Splitted
Welcome to C++! This exciting language, which blends the C language with support
for object-oriented programming and for generic programming, became one of the most
important programming languages of the 1990s and continues strongly in the 2000s. Its C
ancestry brings to C++ the tradition of an efficient, compact, fast, and portable language.
Its object-oriented heritage brings C++ a fresh programming methodology, designed to
cope with the escalating complexity of modern programming tasks. Its template features
bring yet another new programming methodology: generic programming.This triple her-
itage is both a blessing and a bane. It makes the language very powerful, but it also means
there’s a lot to learn.
This chapter explores C++’s background further and then goes over some of the
ground rules for creating C++ programs.The rest of the book teaches you to use the
C++ language, going from the modest basics of the language to the glory of object-ori-
ented programming (OOP) and its supporting cast of new jargon—objects, classes, encap-
sulation, data hiding, polymorphism, and inheritance—and then on to its support of
generic programming. (Of course, as you learn C++, these terms will be transformed
from buzzwords to the necessary vocabulary of cultivated discourse.)
10 Chapter 1 Getting Started with C++
produced compact, fast-running programs, along with the ability to address hardware
matters, such as managing communication ports and disk drives.These gifts helped make
C the dominant programming language in the 1980s. Meanwhile, the 1980s witnessed
the growth of a new programming paradigm: object-oriented programming, or OOP, as
embodied in languages such as SmallTalk and C++. Let’s examine these C and OOP a bit
more closely.
The C Language
In the early 1970s, Dennis Ritchie of Bell Laboratories was working on a project to
develop the Unix operating system. (An operating system is a set of programs that manages
a computer’s resources and handles its interactions with users. For example, it’s the operat-
ing system that puts the system prompt onscreen for a terminal-style interface that man-
ages the windows and mice for graphical interfaces and that runs programs for you.) For
this work Ritchie needed a language that was concise, that produced compact, fast pro-
grams, and that could control hardware efficiently.
Traditionally, programmers met these needs by using assembly language, which is
closely tied to a computer’s internal machine language. However, assembly language is a
low-level language—that is, it works directly with the hardware (for instance, accessing
CPU registers and memory locations directly).Thus, assembly language is specific to a
particular computer processor. So if you want to move an assembly program to a different
kind of computer, you may have to completely rewrite the program, using a different
assembly language. It was a bit as if each time you bought a new car, you found that the
designers decided to change where the controls went and what they did, forcing you to
relearn how to drive.
But Unix was intended to work on a variety of computer types (or platforms).That
suggested using a high-level language.A high-level language is oriented toward problem
solving instead of toward specific hardware. Special programs called compilers translate a
high-level language to the internal language of a particular computer.Thus, you can use
the same high-level language program on different platforms by using a separate compiler
for each platform. Ritchie wanted a language that combined low-level efficiency and
hardware access with high-level generality and portability. So building from older
languages, he created C.
C Programming Philosophy
Because C++ grafts a new programming philosophy onto C, we should first take a look
at the older philosophy that C follows. In general, computer languages deal with two
concepts—data and algorithms.The data constitutes the information a program uses and
processes.The algorithms are the methods the program uses (see Figure 1.1). Like most
mainstream languages when C was created, C is a procedural language.That means it
emphasizes the algorithm side of programming. Conceptually, procedural programming
12 Chapter 1 Getting Started with C++
consists of figuring out the actions a computer should take and then using the program-
ming language to implement those actions. A program prescribes a set of procedures for
the computer to follow to produce a particular outcome, much as a recipe prescribes a set
of procedures for a cook to follow to produce a cake.
DATA ALGORITHMS
PROGRAM
Earlier procedural languages, such as FORTRAN and BASIC, ran into organizational
problems as programs grew larger. For example, programs often use branching statements,
which route execution to one or another set of instructions, depending on the result of
some sort of test. Many older programs had such tangled routing (called “spaghetti pro-
gramming”) that it was virtually impossible to understand a program by reading it, and
modifying such a program was an invitation to disaster. In response, computer scientists
developed a more disciplined style of programming called structured programming. C
includes features to facilitate this approach. For example, structured programming limits
branching (choosing which instruction to do next) to a small set of well-behaved con-
structions. C incorporates these constructions (the for loop, the while loop, the do
while loop, and the if else statement) into its vocabulary.
Top-down design was another of the new principles.With C, the idea is to break a large
program into smaller, more manageable tasks. If one of these tasks is still too broad, you
divide it into yet smaller tasks.You continue with this process until the program is com-
partmentalized into small, easily programmed modules. (Organize your study.Aargh! Well,
organize your desk, your table top, your filing cabinet, and your bookshelves.Aargh! Well,
start with the desk and organize each drawer, starting with the middle one. Hmmm, per-
haps I can manage that task.) C’s design facilitates this approach, encouraging you to
The Origins of C++: A Little History 13
develop program units called functions to represent individual task modules.As you may
have noticed, the structured programming techniques reflect a procedural mind-set,
thinking of a program in terms of the actions it performs.