07-05-2012, 11:08 AM
C++ Essentials
c++.pdf (Size: 540.62 KB / Downloads: 28)
Intended Audience
This book introduces C++ as an object-oriented programming language. No
previous knowledge of C or any other programming language is assumed. Readers
www.pragsoft.com Contents xi
who have already been exposed to a high-level programming language (such as C
or Pascal) will be able to skip over some of the earlier material in this book.
Although the book is primarily designed for use in undergraduate computer
science courses, it will be equally useful to professional programmers and hobbyists
who intend to learn the language on their own. The entire book can be easily
covered in 10-15 lectures, making it suitable for a one-term or one-semester
course. It can also be used as the basis of an intensive 4-5 day industrial training
course.
Structure of the Book
The book is divided into 12 chapters. Each chapter has a flat structure, consisting
of an unnumbered sequence of sections, most of which are limited to one or two
pages. The aim is to present each new topic in a confined space so that it can be
quickly grasped. Each chapter ends with a list of exercises. Answers to all of the
exercises are provided in an appendix. Readers are encouraged to attempt as many
of the exercises as feasible and to compare their solutions against the ones
provided.
Preliminaries
This chapter introduces the basic elements of a C++ program. We will use simple
examples to show the structure of C++ programs and the way they are compiled.
Elementary concepts such as constants, variables, and their storage in memory will
also be discussed.
The following is a cursory description of the concept of programming for the
benefit of those who are new to the subject.
Programming
A digital computer is a useful tool for solving a great variety of problems. A
solution to a problem is called an algorithm; it describes the sequence of steps to
be performed for the problem to be solved. A simple example of a problem and an
algorithm for it would be:
Problem: Sort a list of names in ascending lexicographic order.
Algorithm: Call the given list list1; create an empty list, list2, to hold the sorted list.
Repeatedly find the ‘smallest’ name in list1, remove it from list1, and
make it the next entry of list2, until list1 is empty.
An algorithm is expressed in abstract terms. To be intelligible to a computer, it
needs to be expressed in a language understood by it. The only language really
understood by a computer is its own machine language. Programs expressed in
the machine language are said to be executable. A program written in any other
language needs to be first translated to the machine language before it can be
executed.
Compiling a Simple C++ Program
Dialog 1.1 shows how the program in Listing 1.1 is compiled and run in a typical
UNIX environment. User input appears in bold and system response in plain.
The UNIX command line prompt appears as a dollar symbol ($).