Introduction

Learning Languages from Books

Most people you speak to consider programming languages to be more difficult than human, or natural, languages. In many ways this is true. Programming is the art of preparing precise instructions for machines. These machines are very fast and obedient, but they are incredibly stupid, so everything has to be broken down into steps; nothing can be assumed. Also, most kinds of programming involve doing business or engineering calculations, so most people do not need to program. If setting a VCR feels like programming, somebody has not done his or her job because one of the goals of programming is to eliminate programming. But part of the trouble is that learning to program is often like learning Latin: It involves a lot of grammar and vocabulary, little practice, and no real-life applications. (Living languages, such as school French, can also be successfully killed by these problems.) Living human languages are best learned by speaking to people who know the language, and programming languages are best learned through interactive conversation with computers. Therefore, this book is a conversational course in C++.

Why learn C++? Despite the excitement surrounding Java, C++ remains the foremost industrial-strength programming language. It is incredibly adaptable. It is not the easiest language to learn and use, but you do not have to learn it all at once. Right from the start you can write (and read) interesting code that can do useful things.

By Example

People learn best by example. It's important to read as much good code as possible, just as reading lots of English is the only way to learn to write it. This book presents nontrivial applications of C++ that are interesting, and the case studies at the end of each chapter show C++ in use. I have provided the UnderC C++ interactive system for this edition. You should continuously play with the language, using UnderC, until you have enough confidence in the bricks to build your own houses. In addition, this book comes with the GNU Compiler Collection (GCC), which contains the most popular free C++ compiler.

The C++ Standard

C++ has been around for nearly 20 years , but it has only become an international standard in the past 5 years. Excellent free compilers are available that support that standard, so there has never been a better time to learn C++. Previously, each implementation of C++ had its own code libraries, so portability was a problem.

The strength of a language lies in its libraries. C++ has a powerful and elegant standard library that is available everywhere. Standard C++ is a high-level language that comes complete with the tools for most programming tasks.

The C Family of Languages: Some History

The history of C++ is interesting because it helps to understand where it comes from. On the one hand, there is a tradition of low-level power and efficiency that comes from C, and on the other hand it is a high-level object-oriented language.

C: Close to the Machine

The ancestor of C++ is C, which has been around since the early 1970s. It was developed by Dennis Ritchie at AT&T Bell Labs as a lean, fast language that is rich in low-level machine operations (such as bit twiddling) but can be moved (that is, ported) easily to other machines. As soon as UNIX was rewritten in C, it could itself be ported to many different architectures.

There is a certain cowboy spirit about C: You don't need to be too worried about data type safety if you know what you're doing. C is a very small language. It has no built-in I/O and file access or string manipulation capabilities, unlike BASIC, for example, which has PRINT and INPUT. In C, everything is done with libraries. C is often used to program devices as small as 12KB washing-machine controllers.

C++: “C with Classes”

C++ grew out of C in the early 1980s at Bell Labs. Bjarne Stroustrup was doing research in simulation, and he was inspired by the class concept of the Simula language. The original compiler translated C++ to C, but modern compilers go directly to machine code. The modern C++ language has grown throughout two decades from not having templates, exception handling, or multiple inheritance, to becoming a fine, stable adult.

C++ remains true to its C roots. It still relies on libraries for all its I/O functions and runs on small devices such as cell phones as well as on continentwide telephone networks. To this day, C++ remains incredibly backward compatible with C. A programmer can choose to get close to the machine or operate on a very high level; this flexibility is what gives C++ its special power.

Java: Universal Language

In the early 1990s, researchers at Sun Microsystems were looking at a reliable way to build the next generation of consumer devices. James Gosling and Patrick Naughton developed Java, which is syntactically similar to C++ (that is, it uses the same punctuation and many of the same keywords) but is designed to be a pure object-oriented language.

In consumer devices and Internet services, reliability, security, and portability are the key concepts. Therefore, Java omitted things like C++ pointers, which were error prone and open to abuse, and it generated an intermediate bytecode, which runs on a virtual machine. The Java Virtual Machine (JVM) also provides a “sandbox” in which small programs (called applets) can run safely without compromising security. There is a large library of code available for writing portable graphical user interface programs. Java programs can download new code dynamically (that is, as they are running).

Java is the property of Sun, which in 2001 won a court case against Microsoft for its modifications of Java, claiming that Microsoft was violating the terms of the Sun license.

C#: New Kid on the Block

In many ways, Microsoft's answer to Java is C#, pronounced “C sharp.” Because Sun has effectively shut Microsoft out of more advanced Java versions, the company has been seeking a new platform for portable Internet services; this platform, which has recently been implemented, is called .NET. Microsoft has been thinking about this platform as long as Sun has; it was working on a similar concept, called Blackbird, at the same time that Java came out.

C# is a next-generation Java-like language that generates portable virtual machine code. Many people are asking whether we really need another programming language, but this splitting is probably inevitable because so many programming languages are owned by corporations, rather than being open standards, as C++ is.

UnderC: An Interactive C++

My goal in implementing UnderC was to produce a solid but interactive C++ system that would run a “pocket” version of the standard library. It is a half-compiler—source is compiled to intermediate stack code, which is executed immediately—and this is fast enough for most purposes. Programs can then be built by using compilers such as GCC if raw speed is required, and UnderC can link with dynamic link libraries (also known as shared libraries) that are compiled with any language.

You learn to program by interacting with computers, in the same way that you can learn French by conversing with a French person. Traditional compilers slow down this person–computer interaction by the compile-link-go cycle, in which the whole program is built and linked into an executable program; for even small programs, this cycle takes a second or two. An interpreter, on the other hand, brings up a program practically instantaneously. A magic number for humans is 300 milliseconds; anything more than this, and we start being conscious of a time lag. As programs get larger, the build cycle gets longer, and it gets harder to keep the conversation going, but because UnderC has no link phase, the conversation remains instant. Furthermore, a programmer can immediately test small parts of a system by using the interactive prompt; there is no need to write a 30-line program to test a single line. With UnderC, experimentation becomes less painful, and a kind of conversational flow develops, with no pause between typing and response.

How This Book Is Organized

The book is organized into two parts. Part I concentrates on C++ arithmetic expressions, program flow control, and functions—what is often called structured programming. In Part I you will learn how to do calculations and make decisions, and you will learn how to output the results. Structured programming is the art of organizing actions, using divide-and-conquer techniques to break complex operations into simpler ones.

Part II introduces object-oriented programming, which sees programs as consisting of objects that manage their own data and communicate with each other. In structured programming, there is a dictatorship of functions, and in object-oriented programming there is a democracy of objects. Neither programming approach, of course, is the full story. Some problems—for instance, those that handle simple tasks such as adding up numbers in a file—work best with structured programming.

Some experts would prefer to go straight to object-orientation and not worry about “obsolete” structured programming methods. In my experience with maintaining (bad) object-oriented programs, there is a lot of redundant code, mainly because the programmers never learned to organize their functions and separate out any common code. Also, to understand object-orientation you need to see how the traditional methods fail. Two case studies in this book tackle the same problem (drawing graphical shapes) from the two different perspectives to show the limitations of structured programming and how object-orientation can produce better programs.

Who Should Use This Book

This book does not assume any previous programming experience, although of course any exposure to other programming languages is very useful. Anybody wishing to seriously learn C++ will find everything they need to get started.

What the Reader Should Expect

All C++ language and library concepts in this book are illustrated with examples. There is a lot of code because it is important to see C++ in action. This book is a complete learning kit and contains a C++ interpreter (UnderC) for interactive exercises and two industrial-strength compilers (GCC and Borland) for building C++ programs.

What the Reader Should Not Expect

First of all, don't expect to master C++ in a few weeks. After all, no one expects a person to master a human language like Spanish immediately. But it will not take long to learn enough to write useful programs; it isn't necessary to master C++ to be a competent programmer. Mastery takes time and happens when you're using the language to do real things.

Second, this book is not a reference book. It is a tutorial introduction, and I did not want to confuse you with unnecessary detail. But as you continue to learn C++, you will need a good authority to consult. The classic is, of course, The C++ Programming Language, by Bjarne Stroustrup (3rd edition, Addison-Wesley, 1997), which discusses all features of the language in detail, with more than 300 pages devoted to the standard libraries alone.

Conventions Used in This Book

This book uses several common conventions to help teach C++. Here is a summary of those conventions:

Examples are indicated by the icon shown at the left of this sentence.


The typographical conventions used in this book include the following:

  • Commands and computer output appear in a monospaced computer font.

  • Words you type appear in a boldfaced computer font.

  • Italics are used to introduce you to new terms.

In addition to typographical conventions, the following special elements are included to set off different types of information to make them easily recognizable:

NOTE

Special notes augment the material you read in each chapter. These notes clarify concepts and procedures.


TIP

Information that offers shortcuts and solutions to common problems is highlighted as a tip.


CAUTION

Cautions warn you about roadblocks that sometimes appear when working with C++. Reading the caution sections should help steer you away from trouble and wasted time.


Where to Find the Code

If you install the software on the accompanying CD-ROM, the example code for each chapter will be copied onto your hard drive. For instance, if you have installed to c:ccbx, then Chapter 4's example code will be in c:ccbxchap4. As well as C++ source files, there are Quincy 2000 project files (.prj) for the case studies.

The CD-ROM will install the GNU C++ compiler GCC 2.95.2 and all the tools you need to do commmand-line and graphics programs in Windows. I recommend that you also get the Borland Free C++ compiler. Please see Appendix D, “Compiling C++ Programs and DLLs with GCC and BCC32” for details on where to find up-to-date versions of these compilers. If you are running Linux, your system probably already has the GNU C++ compiler. A command-line version of UnderC without graphics will be available for Linux early in March 2002; please consult the C++ By Example site listed below.

All updates, bug fixes, and general information can be found at http://home.mweb.co.za/sd/sdonovan/ccbx.htm.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.240.196