Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Discover millions of ebooks, audiobooks, and so much more with a free trial

From $11.99/month after trial. Cancel anytime.

Beginning Programming with C++ For Dummies
Beginning Programming with C++ For Dummies
Beginning Programming with C++ For Dummies
Ebook600 pages7 hours

Beginning Programming with C++ For Dummies

Rating: 4 out of 5 stars

4/5

()

Read preview

About this ebook

An ideal starting point to get a strong grasp of thefundamentals of C++

C++ is an object-oriented programming language commonly adoptedby would-be programmers. This book explores the basic developmentconcepts and techniques of C++ and explains the "how" and "why" ofC++ programming from the ground up.

You'll discover what goes into creating a program, as well ashow to put the various pieces together, deal with standardprogramming challenges, handle debugging, and make it all work.

  • Details the basics of C++ programming and explores the "how"and "why" of this object-oriented language
  • Addresses the various components that go into creating aprogram with C++
  • Walks you through common challenges of C++ programming

Assuming no prior experience, Beginning Programming with C++For Dummies is a fun and friendly guide to learning the C++language.

Note: CD-ROM/DVD and other supplementary materials arenot included as part of eBook file.

LanguageEnglish
PublisherWiley
Release dateJul 20, 2010
ISBN9780470909508
Beginning Programming with C++ For Dummies

Read more from Stephen R. Davis

Related to Beginning Programming with C++ For Dummies

Related ebooks

Programming For You

View More

Related articles

Reviews for Beginning Programming with C++ For Dummies

Rating: 3.7857142857142856 out of 5 stars
4/5

14 ratings3 reviews

What did you think?

Tap to rate

Review must be at least 10 words

  • Rating: 5 out of 5 stars
    5/5
    Describes in detail with examples on how the code constructs work
  • Rating: 5 out of 5 stars
    5/5
    Really love it.....
    very helpful.......
  • Rating: 4 out of 5 stars
    4/5
    that's grt...

Book preview

Beginning Programming with C++ For Dummies - Stephen R. Davis

Part I

Let’s Get Started

617977-pp0101.eps

In this part . . .

You will learn what it means to program a computer. You will also get your first taste of programming — I take you through the steps to enter, build, and execute your first program. It will all be a bit mysterious in this part, but things will clear up soon, I promise.

Chapter 1

What Is a Program?

In This Chapter

Understanding programs

Writing your first program

Looking at computer languages

In this chapter, you will learn what a program is and what it means to write a program. You’ll practice on a Human Computer. You’ll then see some program snippets written for a real computer. Finally, you’ll see your first code snippet written in C++.

Up until now all of the programs running on your computer were written by someone else. Very soon now, that won’t be true anymore. You will be joining the ranks of the few, the proud: the programmers.

How Does My Son Differ from a Computer?

A computer is an amazingly fast but incredibly stupid machine. A computer can do anything you tell it (within reason), but it does exactly what it’s told — nothing more and nothing less.

In this respect, a computer is almost the exact opposite of a human: humans respond intuitively. When I was learning a second language, I learned that it wasn’t enough to understand what was being said — it’s just as important and considerably more difficult to understand what was left unsaid. This is information that the speaker shares with the listener through common experience or education — things that don’t need to be said.

For example, I say things to my son like, Wash the dishes (for all the good it does me). This seems like clear enough instructions, but the vast majority of the information contained in that sentence is implied and unspoken.

Let’s assume that my son knows what dishes are and that dirty dishes are normally in the sink. But what about knives and forks? After all, I only said dishes, I didn’t say anything about eating utensils, and don’t even get me started on glassware. And did I mean wash them manually, or is it okay to load them up into the dishwasher to be washed, rinsed, and dried automatically?

But the fact is, Wash the dishes is sufficient instruction for my son. He can decompose that sentence and combine it with information that we both share, including an extensive working knowledge of dirty dishes, to come up with a meaningful understanding of what I want him to do — whether he does it or not is a different story. I would guess that he can perform all the mental gymnastics necessary to understand that sentence in about the same amount of time that it takes me to say it — about 1 to 2 seconds.

A computer can’t make heads or tails out of something as vague as wash the dishes. You have to tell the computer exactly what to do with each different type of dish, how to wash a fork, versus a spoon, versus a cup. When does the program stop washing a dish (that is, how does it know when a dish is clean)? When does it stop washing (that is, how does it know when it’s finished)?

My son has gobs of memory — it isn’t clear exactly how much memory a normal human has, but it’s boat loads. Unfortunately, human memory is fuzzy. For example, witnesses to crimes are notoriously bad at recalling details even a short time after the event. Two witnesses to the same event often disagree radically on what transpired.

Computers also have gobs of memory, and that’s very good. Once stored, a computer can retrieve a fact as often as you like without change. As expensive as memory was back in the early 1980s, the original IBM PC had only 16K (that’s 16 thousand bytes). This could be expanded to a whopping 64K. Compare this with the 1GB to 3GB of main storage available in most computers today (1GB is one billion bytes).

As expensive as memory was, however, the IBM PC included extra memory chips and decoding hardware to detect a memory failure. If a memory chip went bad, this circuitry was sure to find it and report it before the program went haywire. This so-called Parity Memory was no longer offered after only a few years, and as far as I know, it is unavailable today except in specific applications where extreme reliability is required — because the memory boards almost never fail.

On the other hand, humans are very good at certain types of processing that computers do poorly, if at all. For example, humans are very good at pulling the meaning out of a sentence garbled by large amounts of background noise. By contrast, digital cell phones have the infuriating habit of just going silent whenever the noise level gets above a built-in threshold.

Programming a Human Computer

Before I dive into showing you how to write programs for computer consumption, I start by showing you a program to guide human behavior so that you can better see what you’re up against. Writing a program to guide a human is much easier than writing programs for computer hardware because we have a lot of familiarity with and understanding of humans and how they work (I assume). We also share a common human language to start with. But to make things fair, assume that the human computer has been instructed to be particularly literal — so the program will have to be very specific. Our guinea pig computer intends to take each instruction quite literally.

The problem I have chosen is to instruct our human computer in the changing of a flat tire.

The algorithm

The instructions for changing a flat tire are straightforward and go something like the following:

1. Raise the car.

2. Remove the lug nuts that affix the faulty tire to the car.

3. Remove the tire.

4. Mount the new tire.

5. Install the lug nuts.

6. Lower the car.

(I know that technically the lug nuts hold the wheel onto the car and not the tire, but that distinction isn’t important here. I use the terms wheel and tire synonymously in this discussion.)

As detailed as these instructions might seem to be, this is not a program. This is called an algorithm. An algorithm is a description of the steps to be performed, usually at a high level of abstraction. An algorithm is detailed but general. I could use this algorithm to repair any of the flat tires that I have experienced or ever will experience. But an algorithm does not contain sufficient detail for even our intentionally obtuse human computer to perform the task.

The Tire Changing Language

Before we can write a program, we need a language that we can all agree on. For the remainder of this book, that language will be C++, but I use the newly invented TCL (Tire Changing Language) for this example. I have specifically adapted TCL to the problem of changing tires.

TCL includes a few nouns common in the tire-changing world:

car

tire

nut

jack

toolbox

spare tire

wrench

TCL also includes the following verbs:

grab

move

release

turn

Finally, the TCL-executing processor will need the ability to count and to make simple decisions.

This is all that the tire-changing robot understands. Any other command that’s not part of Tire Changing Language generates a blank stare of incomprehension from the human tire-changing processor.

The program

Now it’s time to convert the algorithm, written in everyday English, into a program written in Tire Changing Language. Take the phrase, Remove the lug nuts. Actually, quite a bit is left unstated in that sentence. The word remove is not in the processor’s vocabulary. In addition, no mention is made of the wrench at all.

The following steps implement the phrase Remove a lug nut using only the verbs and nouns contained in Tire Changing Language:

1. Grab wrench.

2. Move wrench to lug nut.

3. Turn wrench counterclockwise five times.

4. Move wrench to toolbox.

5. Release wrench.

I didn’t explain the syntax of Tire Changing Language. For example, the fact that every command starts with a single verb or that the verb grab requires a single noun as its object and that turn requires a noun, a direction, and a count of the number of turns to make. Even so, the program snippet should be easy enough to read (remember that this is not a book about Tire Changing Language).

warning_bomb.eps You can skate by on Tire Changing Language, but you will have to learn the grammar of each C++ command.

The program begins at Step 1 and continues through each step in turn until reaching Step 5. In programming terminology, we say that the program flows from Step 1 through Step 5. Of course, the program’s not going anywhere — the processor is doing all the work, but the term program flow is a common convention.

Even a cursory examination of this program reveals a problem: What if there is no lug nut? I suppose it’s harmless to spin the wrench around a bolt with no nut on it, but doing so wastes time and isn’t my idea of a good solution. The Tire Changing Language needs a branching capability that allows the program to take one path or another depending upon external conditions. We need an IF statement like the following:

1. Grab wrench.

2. If lug nut is present

3. {

4. Move wrench to lug nut.

5. Turn wrench counterclockwise five times.

6. }

7. Move wrench to toolbox.

8. Release wrench.

The program starts with Step 1 just as before and grabs a wrench. In the second step, however, before the program waves the wrench uselessly around an empty bolt, it checks to see if a lug nut is present. If so, flow continues on with Steps 3, 4, and 5 as before. If not, however, program flow skips these unnecessary steps and goes straight on to Step 7 to return the wrench to the toolbox.

In computerese, you say that the program executes the logical expression is lug nut present? This expression returns either a true (yes, the lug nut is present) or a false (no, there is no lug nut there).

tip.eps What I call steps, a programming language would normally call a statement. An expression is a type of statement that returns a value, such as 1 + 2 is an expression. A logical expression is an expression that returns a true or false value, such as is the author of this book handsome? is true.

remember.eps The braces in Tire Changing Language are necessary to tell the program which steps are to be skipped if the condition is not true. Steps 4 and 5 are executed only if the condition is true.

I realize that there’s no need to grab a wrench if there’s no lug to remove, but work with me here.

This improved program still has a problem: How do you know that 5 turns of the wrench will be sufficient to remove the lug nut? It most certainly will not be for most of the tires with which I am familiar. You could increase the number of turns to something that seems likely to be more than enough, say 25 turns. If the lug nut comes loose after the twentieth turn, for example, the wrench will turn an extra 5 times. This is a harmless but wasteful solution.

A better approach is to add some type of loop and test statement to the Tire Changing Language:

1. Grab wrench.

2. If lug nut is present

3. {

4. Move wrench to lug nut.

5. While (lug nut attached to car)

6. {

7. Turn wrench counterclockwise one turn.

8. }

9. }

10. Move wrench to toolbox.

11. Release wrench.

Here the program flows from Step 1 through Step 4 just as before. In Step 5, however, the processor must make a decision: Is the lug nut attached? On the first pass, we will assume that the answer is yes so that the processor will execute Step 7 and turn the wrench counterclockwise one turn. At this point, the program returns to Step 5 and repeats the test. If the lug nut is still attached, the processor repeats Step 7 before returning to Step 5 again. Eventually, the lug nut will come loose and the condition in Step 5 will return a false. At this point, control within the program will pass on to Step 9, and the program will continue as before.

This solution is superior to its predecessor: It makes no assumptions about the number of turns required to remove a lug nut. It is not wasteful by requiring the processor to turn a lug nut that is no longer attached, nor does it fail because the lug nut is only half removed.

As nice as this solution is, however, it still has a problem: It removes only a single lug nut. Most medium-sized cars have five nuts on each wheel. We could repeat Steps 2 through 9 five times, once for each lug nut. However, this doesn’t work very well either. Most compact cars have only four lug nuts, and large pickups have up to eight.

The following program expands our grammar to include the ability to loop across lug nuts. This program works irrespective of the number of lug nuts on the wheel:

1. Grab wrench.

2. For each lug bolt on wheel

3. {

4. If lug nut is present

5. {

6. Move wrench to lug nut.

7. While (lug nut attached to car)

8. {

9. Turn wrench counterclockwise one turn.

10. }

11. }

12. }

13. Move wrench to toolbox.

14. Release wrench.

This program begins just as before with the grabbing of a wrench from the toolbox. Beginning with Step 2, however, the program loops through Step 12 for each lug nut bolt on the wheel.

Notice how Steps 7 through 10 are still repeated for each wheel. This is known as a nested loop. Steps 7 through 10 are called the inner loop, while Steps 2 through 12 are the outer loop.

The complete program consists of the addition of similar implementations of each of the steps in the algorithm.

Computer processors

Removing the wheel from a car seems like such a simple task, and yet it takes 11 instructions in a language designed specifically for tire changing just to get the lug nuts off. Once completed, this program is likely to include over 60 or 70 steps with numerous loops. Even more if you add in logic to check for error conditions like stripped or missing lug nuts.

Think of how many instructions have to be executed just to do something as mundane as move a window about on the display screen (remember that a typical screen may have 1280 x 1024 or a little over a million pixels or more displayed). Fortunately, though stupid, a computer processor is very fast. For example, the processor that’s in your PC can likely execute several billion instructions per second. The instructions in your generic processor don’t do very much — it takes several instructions just to move one pixel — but when you can rip through a billion or so at a time, scrolling a mere million pixels becomes child’s play.

The computer will not do anything that it hasn’t already been programmed for. The creation of a Tire Changing Language was not enough to replace my flat tire — someone had to write the program instructions to map out step by step what the computer will do. And writing a real-world program designed to handle all of the special conditions that can arise is not an easy task. Writing an industrial-strength program is probably the most challenging enterprise you can undertake.

So the question becomes: Why bother? Because once the computer is properly programmed, it can perform the required function repeatedly, tirelessly, and usually at a greater rate than is possible under human control.

Computer Languages

The Tire Changing Language isn’t a real computer language, of course. Real computers don’t have machine instructions like grab or turn. Worse yet, computers think using a series of ones and zeros. Each internal command is nothing more than a sequence of binary numbers. Real computers have instructions like 01011101, which might add 1 to a number contained in a special purpose register. As difficult as programming in TCL might be, programming by writing long strings of numbers is even harder.

technicalstuff.eps The native language of the computer is known as machine language and is usually represented as a sequence of numbers written either in binary (base 2) or hexadecimal (base 16). The following represents the first 64 bytes from the Conversion program in Chapter 3.

: 01010101 10001001 11100101 10000011 11100100 11110000 10000011 11101100

: 00100000 11101000 00011010 01000000 00000000 00000000 11000111 01000100

:00100100 00000100 00100100 01110000 01000111 00000000 11000111 00000100

:00100100 10000000 01011111 01000111 00000000 11101000 10100110 10001100

:00000110 00000000 10001101 01000100 00100100 00010100 10001001 01000100

Fortunately, no one writes programs in machine language anymore. Very early on, someone figured out that it is much easier for a human to understand ADD 1,REG1 as add 1 to the value contained in register 1, rather than 01011101. In the post-machine language era, the programmer wrote her programs in this so-called assembly language and then submitted it to a program called an assembler that converted each of these instructions into their machine-language equivalent.

The programs that people write are known as source code because they are the source of all evil. The ones and zeros that the computer actually executes are called object code because they are the object of so much frustration.

technicalstuff.eps The following represents the first few assembler instructions from the Conversion program when compiled to run on an Intel processor executing Windows. This is the same information previously shown in binary form.

: push %ebp

: mov %esp,%ebp

: and $0xfffffff0,%esp

: sub $0x20,%esp

: call 0x40530c <__main>

: movl $0x477024,0x4(%esp)

: movl $0x475f80,(%esp)

: call 0x469fac

: lea 0x14(%esp),%eax

: mov %eax,0x4(%esp)

This is still not very intelligible, but it’s clearly a lot better than just a bunch of ones and zeros. Don’t worry — you won’t have to write any assembly language code in this book either.

remember.eps The computer does not actually ever execute the assembly language instructions. It executes the machine instructions that result from converting the assembly instructions.

High level languages

Assembly language might be easier to remember, but there’s still a lot of distance between an algorithm like the tire-changing algorithm and a sequence of MOVEs and ADDs. In the 1950s, people started devising progressively more expressive languages that could be automatically converted into machine language by a program called a compiler. These were called high level languages because they were written at a higher level of abstraction than assembly language.

One of the first of these languages was COBOL (Common Business Oriented Language). The idea behind COBOL was to allow the programmer to write commands that were as much like English sentences as possible. Suddenly programmers were writing sentences like the following to convert temperature from Celsius to Fahrenheit (believe it or not, this is exactly what the machine and assembly language snippets shown earlier do):

INPUT CELSIUS_TEMP

SET FAHRENHEIT_TEMP TO CELSIUS_TEMP * 9/5 + 32

WRITE FAHRENHEIT_TEMP

The first line of this program reads a number from the keyboard or a file and stores it into the variable CELSIUS_TEMP. The next line multiplies this number by 9//5 and adds 32 to the result to calculate the equivalent temperature in Fahrenheit. The program stores this result into a variable called FAHRENHEIT_TEMP. The last line of the program writes this converted value to the display.

People continued to create different programming languages, each with its own strengths and weaknesses. Some languages, like COBOL, were very wordy but easy to read. Other languages were designed for very specific areas like database languages or the languages used to create interactive Web pages. These languages include powerful constructs designed for one specific problem area.

The C++ language

C++ (pronounced C plus plus, by the way) is a symbolically oriented high level language. C++ started out life as simply C in the 1970s at Bell Labs. A couple of guys were working on a new idea for an operating system known as Unix (the predecessor to Linux and Mac OS and still used across industry and academia today). The original C language created at Bell Labs was modified slightly and adopted as a worldwide ISO standard in early 1980. C++ was created as an extension to the basic C language mostly by adding the features that I discuss in Parts V and VI of this book.When I say that C++ is symbolic, I mean that it isn’t very wordy, preferring to use symbols rather than long words like in COBOL. However, C++ is easy to read once you are accustomed to what all the symbols mean. The same Celsius to Fahrenheit conversion code shown in COBOL earlier appears as follows in C++:

cin >> celsiusTemp;

fahrenheitTemp = celsiusTemp * 9 / 5 + 32;

cout << fahrenheitTemp;

The first line reads a value into the variable celsiusTemp. The subsequent calculation converts this Celsius temperature to Fahrenheit like before, and the third line outputs the result.

C++ has several other advantages compared with other high level languages. For one, C++ is universal. There is a C++ compiler for almost every computer in existence.

In addition, C++ is efficient. The more things a high level language tries to do automatically to make your programming job easier, the less efficient the machine code generated tends to be. That doesn’t make much of a difference for a small program like most of those in this book, but it can make a big difference when manipulating large amounts of data, like moving pixels around on the screen, or when you want blazing real-time performance. It’s not an accident that Unix and Windows are written in C++ and the Macintosh O/S is written in a language very similar to C++.

Chapter 2

Installing Code::Blocks

In This Chapter

Reviewing the compilation process

Installing the Code::Blocks development environment

Testing your installation with a default program

Reviewing the common installation errors

In this chapter, you will review what it takes to create executable programs from C++ source code that you can run on the Windows, Linux, or Macintosh computer. You will then install the Code::Blocks integrated development environment used in the remainder of the book, and you will build a default test program to check out your installation. If all is working, by the time you reach the end of this chapter, you will be ready to start writing and building C++ programs of your own — with a little help, of course!

Reviewing the Compilation Process

You need two programs to create your own C++ programs. First, you need a text editor that you can use to enter your C++ instructions. Any editor capable of generating straight ASCII text letters will work. I have written programs using the Notepad editor that comes with Windows. However, an editor that knows something about the syntax of C++ is preferable since it can save you a lot of typing and sometimes highlight mistakes that you might be making as you type, in much the same way that a spelling checker highlights misspelled words in a word processor.

The second program you will need is a compiler that converts your C++ source statements into machine language that the computer can understand and interpret. This process of converting from source C++ statements to object machine code is called building. Graphically, the process looks something like that shown in Figure 2-1.

The process of building a program actually has two steps: The C++ compiler first converts your C++ source code statements into a machine executable format in a step known as compiling. It then combines the machine instructions from your program with instructions from a set of libraries that come standard with C++ in a second step known as linking to create a complete executable program.

Figure 2-1: The C++ program development process.

617977-fg0201.eps

Most C++ compilers these days come in what is known as an Integrated Development Environment or IDE. These IDEs include the editor, the compiler, and several other useful development programs together in a common package. Not only does this save you from the need to purchase these programs separately, but combining them into a single package produces several productivity benefits. First, the editor can invoke the compiler quickly without the need for you to switch back and forth manually. In addition, the editors in most IDEs provide quick and efficient means for finding and fixing coding errors.

Some IDEs include visual programming tools that allow the programmer to draw common windows such as dialog boxes on the display — the IDE generates the C++ code necessary to display these boxes automatically.

remember.eps As nice as that sounds, the automatically generated code only displays the windows. A programmer still has to generate the real code that gets executed whenever the operator selects buttons within those windows.

Invariably, these visual IDEs are tightly coupled into one or the other operating system. For example, the popular Visual Studio is strongly tied into the .NET environment in Windows. It is not possible to use Visual Studio without learning the .NET environment and something about Windows along with C++ (or one of the other .NET languages). In addition, the resulting programs only run in a .NET environment.

onthecd.eps In this book, you will use a public domain C++ IDE known as Code::Blocks. Versions of Code::Blocks exist for Windows, Linux, and MacOS — a version for Windows is included on the CD-ROM accompanying this book. Versions of Code::Blocks for Macintosh and Linux are available for free download at www.codeblocks.org.

You will use Code::Blocks to generate the programs in this book. These programs are known as Console Applications since they take input from and display text back to a console window. While this isn’t as sexy as windowed development, staying with Console Applications will allow you to focus on C++ and not be distracted by the requirements of a windowed environment. In addition, using Console Applications will allow the programs in the book to run the same on all environments that are supported by Code::Blocks.

Installing Code::Blocks

onthecd.eps Beginning Programming with C++ For Dummies includes a version of Code::Blocks for Windows on the CD-ROM. This section provides detailed installation instructions for this version. The steps necessary to download and install versions of Code::Blocks from www.codeblocks.org will be similar.

1. Insert the enclosed CD-ROM into your computer.

That’s straightforward enough.

2. Read the End User License Agreement (EULA) and select Accept.

3. Select the Software tab and then select Code::Blocks to install the Code::Blocks environment.

On some versions of Windows, you may see a message appear that An unidentified program wants access to your computer. Of course, that unidentified program is the Code::Blocks Setup program.

4. Select Allow.

Setup now unpacks the files it needs to start and run the Code::Blocks Setup Wizard. This may take about a minute. Once it finishes, the startup window shown in Figure 2-2 appears.

5. Close any other programs that you may be executing and select Next.

The Setup Wizard displays the generic End User License Agreement (EULA). There’s nothing much here to get excited about.

Figure 2-2: The Code::Blocks Setup Wizard guides you through the installation process.

617977-fg0202.eps

6. Select I Agree.

The Setup Wizard then displays a list of the components that you may choose to install. The defaults are okay, but you may want to also check the Desktop Shortcut option as shown in Figure 2-3. Doing this provides an icon on the desktop that you can use to start Code::Blocks without going through the Program Files menu.

Figure 2-3: Checking Desktop Shortcut creates an icon that you can use to start Code::Blocks more quickly.

617977-fg0203.eps

7. Select Next.

The next window asks you to choose the install location. This window also tells you how much hard disk space Code::Blocks requires (about 150MB, depending upon the options you’ve selected) and how much space is available on your hard drive. If you don’t have enough free disk space, you’ll have to delete some of those YouTube videos you’ve captured to make room before you continue.

8. The default install location is fine, so once you have enough disk space, select Install.

At this point, the Code::Blocks Setup Wizard really goes to work. It extracts umpteen dozen files that it installs in a myriad of subdirectories too complicated for mere mortals. This process may take several minutes.

9. When the installation is complete, a dialog box appears asking you whether you want to run Code::Blocks now. Select No.

If all has gone well so far, the Installation Complete window shown in Figure 2-4 appears.

Figure 2-4: The Installation Complete window signals that Code::Blocks has been successfully installed.

617977-fg0204.eps
Enjoying the preview?
Page 1 of 1