• Chapters include review questions and programming exercises. Appendix J provides the answers to the review questions.
• The book introduces several topics that are appropriate for computer science courses, including abstract data types (ADTs), stacks, queues, simple lists, simulations, generic programming, and using recursion to implement a divide-and-conquer strategy.
• Most chapters are short enough to cover in a week or less.
• The book discusses
Conventions Used in This Book
This book uses several typographic conventions to distinguish among various kinds of text:
• Code lines, commands, statements, variables, filenames, and program output appear in a computer typeface:
#include
int main()
{
using namespace std;
cout << "What's up, Doc!\n";
return 0;
}
• Program input that you should type appears in bold computer typeface:
Please enter your name:
Plato
• Placeholders in syntax descriptions appear in an
•
Sidebar
A sidebar provides a deeper discussion or additional background to help illuminate a topic.
Tip
Tips present short, helpful guides to particular programming situations.
Caution
A caution alerts you to potential pitfalls.
Note
The notes provide a catch-all category for comments that don’t fall into one of the other categories.
Systems Used to Develop This Book’s Programming Examples
For the record, the C++11 examples in this book were developed using Microsoft Visual C++ 2010 and Cygwin with Gnu g++ 4.5.0, both running under 64-bit Windows 7. The remaining examples were tested with these systems, as well as on an iMac using g++ 4.2.1 under OS X 10.6.8 and on an Ubuntu Linux system using g++ 4.4.1. Most of the pre-C++11 examples were originally developed using Microsoft Visual C++ 2003 and Metrowerks CodeWarrior Development Studio 9 running under Windows XP Professional and checked using the Borland C++ 5.5 command-line compiler and GNU gpp 3.3.3 on the same system, using Comeau 4.3.3 and GNU g++ 3.3.1 under SuSE 9.0 Linux, and using Metrowerks Development Studio 9 on a Macintosh G4 under OS 10.3.
C++ offers a lot to the programmer; learn and enjoy!
1. Getting Started with C++
In this chapter you’ll learn about the following:
• The history and philosophy of C and of C++
• Procedural versus object-oriented programming
• How C++ adds object-oriented concepts to the C language
• How C++ adds generic programming concepts to the C language
• Programming language standards
• 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 heritage 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-oriented programming (OOP) and its supporting cast of new jargon—objects, classes, encapsulation, 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.)
Learning C++: What Lies Before You