Introduction

We imagine that most people come to C++ for one of two reasons. In the first group are those that have C++ thrust upon them, in school or at work. In the second group are those with an interest in writing software who think that C++ may just be the right tool for the job. In either case, you won’t be disappointed—in the language or in this book.

C++ has been around for years and, despite the arrival of newcomers like Java and C#, is still one of the top dogs when it comes to software development. The heaviest of the heavyweights—Microsoft, Adobe, Sun, Intel, Amazon.com, Google, Apple, Nokia—all use C++ to some extent. The language is relatively easy to use (especially when learning it using an introductory text like this one) and is amazingly powerful. You’ll be able to create basic applications today and really far-out ones in months.

Although C++ is a serious programming language, even those without formal training or only modest computer skills can pick up the subject. This book was written as the no-frills, here’s-what-you-really-need-to-know beginner’s guide to the C++ programming language. Absolutely no programming experience is expected of you (including no C experience), but by following the examples and explanations we present here, you’ll have some real-world know-how in no time.

What Is C++?

To understand C++, you must begin with C. The C programming language was created in the 1970s and provided a new, valuable tool for programmers (C arose from the B programming language, but we don’t need to go that far back). The two main benefits of C are performance and portability. C programs tend to be compact and speedy, and most C code can easily be used on many different operating systems.

C is a procedural language, which means that the focus is on an ordered sequence of commands. This is fine, but as you program more and as your applications become larger, reliance upon procedures is less and less efficient.

The C++ programming language was created in the early 1980s by Bjarne Stroustrup, an employee at Bell Labs. C++ was intended as an improved version of C. C++ retains all of the benefits of C—its efficiency, portability, and ability to work with computers on a low level—while adding:

• Support for objects and OOP (see the sidebar)

• Much improved productivity for the programmer

• Fixes for common C problems

This is not to say that there isn’t a need or room for C anymore, just that C++ is quite the upgrade. Which leads us to our next point...

Although C++ is an outgrowth of C, you do not need to know C in order to use this book. If you already know C, that won’t hurt, but you’ll see early on that we always discard problematic and antiquated C techniques if C++ has created a better solution (like using C++ strings instead of C-style strings). C is still a subset of C++, meaning that most C code is valid in C++, but our focus is on programming in proper C++.

How C++ Works

Creating applications in C++ is a multistep process. First you must understand what the end result is: what your application should do. By clearly stating your objectives, you can better determine what variables need to exist, what functionality must be present, and so forth. Every application in the book is prefaced with a brief description of its purpose.

Script i.1. The format of a most basic C++ source file.

1   // hello.cpp - Script i.1
2   // This is a sample
3   // C++ file.
4
5   #include <iostream>
6
7   int main() {
8
9       // Say hello.
10      std::cout << "Hello, World!";
11
12      return 0;
13
14  }

The next step is to begin creating the C++ source code, a plain-text file that will look like Script i.1, for starters. Obviously the main focus of this book is to teach you what code you need to type to create the desired application.

Once you’ve created your source code, it needs to be compiled. Compilation is the process of turning a plain-text file of C++ code into a set of computer-readable instructions. The result of the compilation process—assuming it works—is an executable application, compiled specifically to work on that machine (Figure i.1).

Figure i.1. The hello application (on the right) is the compiled result of the hello.cpp file (on the left).

image

If compilation did not work, you’ll need to debug, debug, and debug. Errors can occur by inadvertently misspelling something, omitting a particular character, or out-and-out misusing a function. Rest assured that all of the code in this book has been debugged, meaning that if you follow the instructions exactly, your code will work too.

Once you have a compiled application, you can run it just as you would any other application: by double-clicking its icon. All of the examples in this book will run in a console or terminal window (Figure i.2).

Figure i.2. Compiling and running the hello program on Windows.

image

What You’ll Need

The requirements for working with C++ are both free and minimal. For starters, you’ll need a computer, but you probably already knew that. It doesn’t matter what kind of computer you have, what operating system it’s running, or really how much memory and hard disk space is available. If your computer can run, say, Mozilla Firefox, it has all the power you need for creating applications in C++.

The most important—in fact, the only—requirement is that you have a qualified C++ compiler on your computer. This can be as simple as g++ (part of the GNU Compiler Collection, gcc), which is freely available, can run on most operating systems, and already comes on most Unix-derivative operating systems. Along with your compiler, you’ll want a text editor and a command-line interface from which you’ll run the compiler. Both of these tools are already present on every operating system.

Although you can use a text editor and a compiler to create your applications, a much easier option is to use an all-in-one integrated development environment (IDE) like Dev-C++ on Windows or Xcode on Mac OS X. Both of these tools are free and allow you to write, compile, debug, and execute your C code all within the one interface. Appendix A, “C++ Tools,” covers the installation of these two tools. In Chapter 1, “Creating a Basic Program,” you’ll learn how to basically use both. We highly recommend you use either Dev-C++ or Xcode, depending on your operating system. Every one of the book’s applications conforms to the latest C++ standard (see the sidebar) and has been tested using both IDEs. If you use either Dev-C++ or Xcode, following the examples will go more smoothly.

From you, the reader, nothing is expected except an interest and willingness to learn C++.

About This Book

As C++ is an outgrowth of C, many C++ programming books assume a good, working knowledge of C. This book does not. The foundations of both C and C++ are covered in the first few chapters, but from a C++ perspective. You will not learn everything about C from this book, but you’ll learn the fundamentals that you’ll need to do true C++ coding.

As with most books on the C++ programming language, an odd structure can arise. Because the whole purpose of C++ is to be able to more easily write applications, many complex notions can be followed quite easily. Therefore, what you’ll occasionally see are advanced ideas being used early in the book—because they are easy to implement—and then fully discussed later. We mention this so that you’ll know in advance that even if you don’t fully understand something early on, in due time the issue should be corrected. Strange as this may seem, it’s a preferable way to cover C++, rather than demonstrating less confusing but technically inaccurate techniques early on.

This book attempts to convey the fundamentals of programming with C++, without going into overwhelming details or bombarding you with irrelevant technicalities. It uses the following conventions to do.

The step-by-step instructions indicate what code you are to type or what other steps you are to take. The specific text you should type is printed in a unique type style to separate it from the main text. For example:

std::cout << "Hello, world!";

Because the column width in this book is narrower than the common text editor or IDE, some lines of code printed in the steps have to be broken where they would not otherwise break in an editor. A small gray arrow indicates when this kind of break occurs. For example:

std::cout << "Hello, world! How are you doing on this fine Sunday afternoon?";

With such code, you should continue to use one line in your scripts, or else you might encounter errors.

The complete C++ code is also written as its own separate script and is numbered by line for reference (see Script i.1). You shouldn’t insert these numbers yourself, because doing so will render your code unusable. Most good text editors and IDEs will number lines for you. In these script blocks, we also highlight in bold the sections that demonstrate new or relevant concepts. (Just so you know, C++ source code files are not normally called “scripts”; that’s just a convention for referring to a document of code within the Visual QuickStart Guide series.)

You will also frequently see images showing the results of running an application (see Figure i.2), displaying a command you need to enter (Figure i.3), or demonstrating a particular subpart of an application. All of the images were taken on either Windows or Mac OS X (the Mac OS X images and steps are similar to those for most Unix and Linux users). The exact appearance of an application will change from one computer to the next, but most of the content should be similar, as will all of the functionality.

Figure i.3. Invoking the g++ compiler in a Mac OS X Terminal window.

image

Finally, here’s what this book doesn’t do: it does not conclude each chapter with a list of questions and potential exercises, as you’ll find in some programming texts. That is just not in keeping with the Visual QuickStart Guide series. But you will see recommendations for how you could modify or apply some techniques and, more important, you will be taught what you must learn about C++, in a practical and manageable way.

Getting Help

Although this book was written with the intent of being the most down-to-earth, basic, get-going-now text around, you may run into problems and desire a little assistance on occasion. Here are some choices, in order of likelihood for getting a fast response (fastest options are listed first):

• Search the Internet.

If you have questions about a particular function, header file, or concept, Google will often get you immediate answers.

• Use a C++ newsgroup or forum.

Appendix B, “Resources,” lists a number of available outlets for when you have a specific question. If you ask a question wisely (see the sidebar), you should get the answer you need in a relatively short time.

• Check out the book’s supporting Web site.

The book’s official Web site can be found at www.DMCInsights.com/cppvqs. There you’ll find all the scripts from the book, links to other resources, and a list of any printing errors.

• Check out the book’s support forum.

Through the book’s supporting Web site, you can find a support forum where readers can post questions, get answers, see what others are doing, and so forth. We (the authors of this book) moderate this forum, which means we’ll answer your question if someone doesn’t beat us to the punch.

• Email the authors.

If all else fails, we gladly welcome your emails at [email protected]. A few words of caution, though: we can’t do your job for you, we can’t debug the 200 lines of code you wrote overnight, and it will take several days for us to get back to you. Still, if you do write, we will respond, and will try to assist as best as possible.

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

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