0% found this document useful (0 votes)
21 views

PDF 14feb24 0858 Splitted

This document provides an introduction to C++, covering its history and origins from C as well as the additional object-oriented and generic programming concepts it incorporates. C++ blends procedural C programming with support for object-oriented programming using classes. It also adds template features for generic programming. Learning C++ requires understanding its C basis as well as its new OOP and generic components. The document discusses C++'s triple heritage from C, OOP languages, and generic programming.

Uploaded by

ks7580713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

PDF 14feb24 0858 Splitted

This document provides an introduction to C++, covering its history and origins from C as well as the additional object-oriented and generic programming concepts it incorporates. C++ blends procedural C programming with support for object-oriented programming using classes. It also adds template features for generic programming. Learning C++ requires understanding its C basis as well as its new OOP and generic components. The document discusses C++'s triple heritage from C, OOP languages, and generic programming.

Uploaded by

ks7580713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Getting Started with C++

In this chapter you’ll learn about the following:


n The history and philosophy of C and of C++
n Procedural versus object-oriented programming
n How C++ adds object-oriented concepts to the C language
n How C++ adds generic programming concepts to the C language
n Programming language standards
n The mechanics of creating a program

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++

Learning C++: What Lies Before You


C++ joins three separate programming categories: the procedural language, represented
by C; the object-oriented language, represented by the class enhancements C++ adds to
C; and generic programming, supported by C++ templates.This chapter looks into those
traditions. But first, let’s consider what this heritage implies about learning C++. One
reason to use C++ is to avail yourself of its object-oriented features.To do so, you need a
sound background in standard C, for that language provides the basic types, operators,
control structures, and syntax rules. So if you already know C, you’re poised to learn
C++. But it’s not just a matter of learning a few more keywords and constructs. Going
from C to C++ involves perhaps more work than learning C in the first place. Also if you
know C, you must unlearn some programming habits as you make the transition to C++.
If you don’t know C, you have to master the C components, the OOP components, and
the generic components to learn C++, but at least you may not have to unlearn pro-
gramming habits. If you are beginning to think that learning C++ may involve some
mind-stretching effort on your part, you’re right.This book will guide you through the
process in a clear, helpful manner, one step at a time, so the mind-stretching will be suffi-
ciently gentle to leave your brain resilient.
C++ Primer Plus approaches C++ by teaching both its C basis and its new compo-
nents, so it assumes that you have no prior knowledge of C.You’ll start by learning the
features C++ shares with C. Even if you know C, you may find this part of the book a
good review.Also it points out concepts that will become important later, and it indicates
where C++ differs from C.After you have a good grounding in the basics of C, you’ll
learn about the C++ superstructure.At that point, you’ll learn about objects and classes
and how C++ implements them.And you will learn about templates.
This book is not intended to be a complete C++ reference; it doesn’t explore every
nook and cranny of the language. But you will learn most of the major features of the
language, including templates, exceptions, and namespaces.
Now let’s take a brief look at some of C++’s background.

The Origins of C++: A Little History


Computer technology has evolved at an amazing rate over the past few decades.Today a
notebook computer can compute faster and store more information than the mainframe
computers of the 1960s. (Quite a few programmers can recall bearing offerings of decks
of punched cards to be submitted to a mighty, room-filling computer system with a
majestic 100KB of memory—far less memory than even a smartphone uses today.) Com-
puter languages have evolved, too.The changes may not be as dramatic, but they are
important. Bigger, more powerful computers spawn bigger, more complex programs,
which, in turn, raise new problems in program management and maintenance.
In the 1970s, languages such as C and Pascal helped usher in an era of structured pro-
gramming, a philosophy that brought some order and discipline to a field badly in need
of these qualities. Besides providing the tools for structured programming, C also
The Origins of C++: A Little History 11

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

1/2 cup butter cream butter


1 cup sugar + gradually, add sugar
2 eggs break eggs
… …

PROGRAM

Figure 1.1 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.

The C++ Shift: Object-Oriented Programming


Although the principles of structured programming improved the clarity, reliability, and
ease of maintenance of programs, large-scale programming still remains a challenge. OOP
brings a new approach to that challenge. Unlike procedural programming, which empha-
sizes algorithms, OOP emphasizes the data. Rather than try to fit a problem to the proce-
dural approach of a language, OOP attempts to fit the language to the problem.The idea
is to design data forms that correspond to the essential features of a problem.
In C++, a class is a specification describing such a new data form, and an object is a par-
ticular data structure constructed according to that plan. For example, a class could
describe the general properties of a corporation executive (name, title, salary, unusual abil-
ities, for example), while an object would represent a specific executive (Guilford Sheep-
blat, vice president, $925,000, knows how to restore the Windows registry). In general, a
class defines what data is used to represent an object and the operations that can be per-
formed on that data. For example, suppose you were developing a computer drawing pro-
gram capable of drawing a rectangle.You could define a class to describe a rectangle.The
data part of the specification could include such things as the location of the corners, the
height and width, the color and style of the boundary line, and the color and pattern used
to fill the rectangle.The operations part of the specification could include methods for
moving the rectangle, resizing it, rotating it, changing colors and patterns, and copying the
rectangle to another location. If you then used your program to draw a rectangle, it would
create an object according to the class specification.That object would hold all the data
values describing the rectangle, and you could use the class methods to modify that rec-
tangle. If you drew two rectangles, the program would create two objects, one for each
rectangle.
The OOP approach to program design is to first design classes that accurately repre-
sent those things with which the program deals. For example, a drawing program might
define classes to represent rectangles, lines, circles, brushes, pens, and the like.The class
definitions, recall, include a description of permissible operations for each class, such as
moving a circle or rotating a line.Then you would proceed to design a program, using
objects of those classes.The process of going from a lower level of organization, such as
classes, to a higher level, such as program design, is called bottom-up programming.
There’s more to OOP than the binding of data and methods into a class definition. For
example, OOP facilitates creating reusable code, and that can eventually save a lot of
work. Information hiding safeguards data from improper access. Polymorphism lets you
create multiple definitions for operators and functions, with the programming context
determining which definition is used. Inheritance lets you derive new classes from old
ones.As you can see, OOP introduces many new ideas and involves a different approach
to programming than does procedural programming. Instead of concentrating on tasks,

You might also like