The best-selling C++ For Dummies book makes C++ easier!
C++ For Dummies, 7th Edition is the best-selling C++ guide on the market, fully revised for the 2014 update. With over 60% new content, this updated guide reflects the new standards, and includes a new Big Data focus that highlights the use of C++ among popular Big Data software solutions. The book provides step-by-step instruction from the ground up, helping beginners become programmers and allowing intermediate programmers to sharpen their skills. The companion website provides all code mentioned in the text, an updated GNU_C++, the new C++ compiler, and other applications. By the end of the first chapter, you will have programmed your first C++ application!
As one of the most commonly used programming languages, C++ is a must-have skill for programmers who wish to remain versatile and marketable. C++ For Dummies, 7th Edition provides clear, concise, expert instruction, which is organized for easy navigation and designed for hands-on learning. Whether you're new to programming, familiar with other languages, or just getting up to speed on the new libraries, features, and generics, this guide provides the information you need.
- Provides you with an introduction to C++ programming
- Helps you become a functional programmer
- Features information on classes, inheritance, and optional features
- Teaches you 10 ways to avoid adding bugs
The book incorporates the newest C++ features into the fundamental instruction, allowing beginners to learn the update as they learn the language. Staying current on the latest developments is a crucial part of being a programmer, and C++ For Dummies, 7th Edition gets you started off on the right foot.
About the Author
Stephen R. Davis is the bestselling author of numerous books and articles, including C# For Dummies. He has been programming for over 30 years and currently works for Booz Allen Hamilton in the area of Homeland Defense.
Read an Excerpt
C++ For Dummies
By Stephen R. Davis
John Wiley & SonsCopyright © 2014 John Wiley & Sons, Ltd
All rights reserved.
Writing Your First C++ Program
In This Chapter
* Finding out about C++
* Installing Code::Blocks on Windows, Ubuntu Linux, or Macintosh OS X
* Creating your first C++ program
* Executing your program
Okay, so here we are: No one here but just you and me. Nothing left to do but get started. Might as well lay out a few fundamental concepts.
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.
Perhaps unfortunately for us, computers don't understand any reasonable human language — they don't speak English either. Okay, I know what you're going to say: "I've seen computers that could understand English." What you really saw was a computer executing a program that could meaningfully understand English.
Computers understand a language variously known as computer language or machine language. It's possible but extremely difficult for humans to speak machine language. Therefore, computers and humans have agreed to sort of meet in the middle, using intermediate languages such as C++. Humans can speak C++ (sort of), and C++ can be converted into machine language for the computer to understand.
Grasping C++ Concepts
A C++ program is a text file containing a sequence of C++ commands put together according to the laws of C++ grammar. This text file is known as the source file (probably because it's the source of all frustration). A C++ source file normally carries the extension .CPP just as an Adobe Acrobat file ends in .PDF or an MS-DOS (remember that?) batch file ends in .BAT.
The point of programming in C++ is to write a sequence of commands that can be converted into a machine-language program that actually does what we want done. This conversion is called compiling and is the job of the compiler. The machine code that you wrote must be combined with some setup and teardown instructions and some standard library routines in a process known as linking. Together, compiling and linking are known as building. The resulting machine-executable files carry the extension .EXE in Windows. They don't carry any particular extension in Linux or Macintosh.
That sounds easy enough — so what's the big deal? Keep going.
To write a program, you need two specialized computer programs. One (an editor) is what you use to write your code as you build your .CPP source file. The other (a compiler) converts your source file into a machine-executable file that carries out your real-world commands (open spreadsheet, make rude noises, deflect incoming asteroids, whatever).
Nowadays, tool developers generally combine compiler and editor into a single package — a development environment. After you finish entering the commands that make up your program, you need only click a button to build the executable file.
Fortunately, there are public-domain C++ environments. I use one of them in this book — the Code::Blocks environment. This editor will work with a lot of different compilers, but the version of Code::Blocks combined with the GNU gcc compiler used to write this book is available for download for Windows, Macintosh, and various versions of Linux, as described in the installation section of this chapter.
Although Code::Blocks is public domain, you're encouraged to pay some small fee to support its further development. You don't have to pay to use Code::Blocks, but you can contribute to the cause if you like. See the Code::Blocks website for details.
I have tested the programs in this book with Code::Blocks 13.12 which comes bundled with gcc version 4.7.1. This version of gcc implements most of the C++ 2011 standard.
You can use different versions of gcc or even different compilers if you prefer, but they may not implement the complete '11 standard. For that reason, 2011 extensions are marked with the '11 icon seen here.
The gcc compiler does not implement any of the extensions added in the C++ 2014 standard as of this writing, but I have included them, where applicable, because some day it will.
Okay, I admit it: This book is somewhat Windows-centric. I have tested all of the programs in the book on Windows 2000/XP/Vista/7/8, Ubuntu Linux, and Macintosh OS X. I flag any differences between operating systems in the text.
In addition, I include installation instructions for each of the above three operating systems in this chapter. Versions of Code::Blocks and gcc are available for other flavors of Linux and other versions of the Macintosh OS. The programs should work with these, as well.
The Code::Blocks/gcc package generates 32-bit programs, but it does not easily support creating "windowed" programs. The programs in this book run from a command line prompt and write out to the command line. As boring as that may sound, I strongly recommend that you work through the examples in this book first to learn C++ before you tackle windowed development. C++ and windows programming are two separate things and (for the sake of your sanity) should remain so in your mind.
Follow the steps in the next section to install Code::Blocks and build your first C++ program. This program's task is to convert a temperature value entered by the user from degrees Celsius to degrees Fahrenheit.
The www.dummies.com/extra/cplusplus website includes the most recent version of the Code::Blocks environment at the time of this writing for Windows, Ubuntu Linux, and Macintosh OS X 10.6 or later. Follow the installation instructions below that apply to your operating system.
The Code::Blocks environment comes in an easy-to-install, compressed executable file that is compatible with all versions of Windows after Windows 2000. Here's the rundown on installing the environment:
1. Download the executable codeblocks-13.12.mingw-setup.exe from www.dummies.com/extra/cplusplus.
Save the executable to your desktop or some other place that you can easily find it.
This includes the 4.71 version of the GCC compiler. This is not the newest version of GCC but it's the version recommended by Code::Blocks. If you want the newer but perhaps slightly buggy 4.81 version, you can download and install codeblocks-13.12.mingw-setupTDM-GCC-481.exe instead. I tested the programs in this book with both versions but I used 4.71 during its writing.
2. Double-click the program once it has completed downloading.
3. Depending on what version of Windows you're using, you may get the ubiquitous "An unidentified program wants access to your computer" warning pop-up. If so, click Allow to get the installation ball rolling.
4. Click Next after closing all extraneous applications as you are warned in the Welcome dialog box to the Code::Blocks Setup Wizard.
5. Read the End User License Agreement (commonly known as the EULA) and then click I Agree if you can live with its provisions.
It's not like you have much choice — the package really won't install itself if you don't accept. Assuming you do click OK, Code::Blocks opens a dialog box showing the installation options. The default options are fine.
6. Click the Next button.
The installation program allows you to install only some subset of the features. You must select at least the Default Install and the MinGW Compiler Suite. The default is to install everything — that's the best choice.
If the MinGW Compiler Suite is not an option, then you must have downloaded a version of Code::Blocks that does not include gcc. This version will not work correctly.
7. Click Install and accept the default Destination Folder.
Code::Blocks commences to copying a whole passel of files to your hard drive. Code::Blocks then asks "Do you want to run Code::Blocks now?"
8. Click Yes to start Code::Blocks.
Code::Blocks now asks which compiler you intend to use. The default is GNU GCC Compiler, which is the proper selection.
9. From within Code::Blocks, choose Settings-Compiler.
10. Select the Compiler Flags tab.
11. Make sure that the following three flags are selected, as shown in Figure 1-1:
Enable All Compiler Warnings
Have g++ Follow the Coming C++0x ISO C++ Language Standard
Have g++ Follow the C++11 ISO C++ Language Standard
The C++ 2011 standard was originally supposed to be the C++ 2008 or 2009 standard. Since it wasn't clear, the standard became known as the 0x standard. The standard wasn't completely accepted until 2011. Within gcc, C++0x and C++11 refer to the same standard.
12. Select the Toolchain Executables tab. Make sure that it appears like Figure 1-2.
The default location for the gcc compiler is the MinGW\bin subdirectory of the Code::Blocks directory.
If the default location is empty, then Code::Blocks does not know where the gcc compiler is, and it will not be able to build your programs. Make sure that you downloaded a version of Code::Blocks that includes gcc and that you included MinGW during the installation. If you are using an existing gcc compiler that you've already installed, then you will need to point Code::Blocks to where it is located on your hard drive.
13. Close the Settings dialog box.
14. Click Next in the Code::Blocks Setup dialog box and then click Finish to complete the setup program.
The setup program exits.
Code::Blocks does not include gcc on Linux, so installation is a two-step process. First you will need to install gcc. Then you can install Code::Blocks.
The gcc compiler is readily available for Linux. Follow these steps to install it:
1. Enter the following commands from a command prompt:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install g++
The standard Ubuntu Linux distribution includes a GNU C compiler, but it does not include the C++ extensions and, in particular, not the C++ 2011 standard extensions. The first two commands update and upgrade the tools you already have. The third command installs C++.
2. Enter the following command from a command prompt:
My Ubuntu 13.04 downloaded GNU C++ version 4.7.3. You'll be fine with version 4.7.1 or later. If you have an earlier version, some of the C++ 2011 features may not work properly, but otherwise, it should be okay.
If you are using Debian Linux, the commands are the same. If you're using Red Hat Linux, replace the command apt-get with yum so that you end up with
sudo yum install g++
Fortunately for all concerned, an Ubuntu-ready version of Code::Blocks is available in the Ubuntu Software Center. Many other versions of Linux include something similar to the Software Center. Follow these steps to install Code::Blocks:
1. Click on the Software Center icon on the Ubuntu desktop.
2. Select Code::Blocks from the list of available software.
This will start the installation process.
Code::Blocks searches your hard drive for your C++ compiler. It should be able to find it without a problem, but if it doesn't, then execute the following steps.
3. Start Code::Blocks.
4. Select Settings-Compiler.
5. Select the Compiler Flags tab.
6. Make sure that the following three flags are selected, as shown in Figure 1-1:
Enable All Compiler Warnings
Have g++ Follow the Coming C++0x ISO C++ Language Standard
Have g++ Follow the C++11 ISO C++ Language Standard
7. Select the Toolchain Executables tab.
8. Select the "..." button.
9. Navigate to /usr, unless you installed your gcc compiler someplace other than the default location of /user/bin.
10. The "C compiler" should be gcc, the "C++ compiler" should be g++ and the "Linker for dynamic libs" should be g++.
11. Select OK to close the dialog box.
The Macintosh version of Code::Blocks relies on the Xcode distribution from Apple for its compiler. I have divided the installation into three separate parts for this reason.
Xcode is a free development package offered by Apple that you will need. Follow these steps to install it first:
1. Open the Safari browser and go to http://developer.apple.com.
2. Click on Download Xcode to get the most recent version.
This will open the Xcode download dialog box shown in Figure 1-3.
3. Click on the Free icon to change it to Install App. Click on it again.
4. Enter your system password (the one you log in with when your Mac boots up).
The icon changes to Installing.
The download and installation takes quite some time, as Xcode is a little over 2GB as of this writing.
Installing the Command Line Tools
As big as Xcode is, you would think that it has everything you need, but you would be wrong. You need one more package from Apple to make your joy complete and to get a working gcc compiler on your Macintosh. Follow these steps to install the Command Line Tools for Xcode:
1. Open the Safari browser and go to http://developer.apple.com/ downloads.
You may be asked to sign up for an Apple Developer ID. Go ahead and do so — it's free.
2. Search for Command Line Tools for Xcode. Select the application shown in Figure 1-4. Click on the Download icon.
3. Double-click on the mpkg package that downloads to install it.
4. Accept all of the default values.
The installation should finish with Installation Was Successful.
Now, you can finish your installation by downloading the Code::Blocks editor:
1. Open the Safari browser and go to www.codeblocks.org/downloads.
2. Click on Downloads-Binaries.
3. Click on Mac OS X.
4. Select either the BerliOS or Sourceforge.net mirror for the most recent version.
At the time of this writing, CodeBlocks-13.12 -mac.zip was the most recent.
5. Install the downloaded Zip file into the Applications folder.
If you have never installed an application from a third-party site, you may need to execute these extra steps before you can do so:
a. Click on System Preferences.
b. Click on Security and Privacy.
c. Click the padlock in the lower-left corner of the dialog box to allow changes.
d. Click on Allow Applications Downloaded from: Anywhere, as shown in Figure 1-5.
Once you have completed the installation of Code::Blocks, you may choose to return to this dialog box and restore the settings to Mac App Store.
6. Double-click on the Code::Blocks icon.
The first time you do this, the Mac OS will ask, "Are you sure you want to open it?"
7. Select Don't Warn Me When Opening Applications on This Disk Image and click Open.
Code::Blocks should start and find the gcc compiler installed with the Command Line Tools.
8. Select the gcc compiler, as shown in Figure 1-6. Click on Set as Default and then click on OK to continue starting Code::Blocks.
Code::Blocks will open with a banner page followed by a menu across the top of the dialog box.
9. Select Settings-Compiler, then click the Have g++ Follow the Coming C++0x ISO C++ Language Standard. Click on OK to close the dialog box.
You are now ready to build your first C++ program.
Creating Your First C++ Program
In this section, you create your first C++ program. You enter the C++ code into a file called CONVERT.CPP and then convert the C++ code into an executable program.
Excerpted from C++ For Dummies by Stephen R. Davis. Copyright © 2014 John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
Table of Contents
Part I: Getting Started with C++ Programming 7
Chapter 1: Writing Your First C++ Program 9
Chapter 2: Declaring Variables Constantly 33
Chapter 3: Performing Mathematical Operations 47
Chapter 4: Performing Logical Operations 53
Chapter 5: Controlling Program Flow 69
Part II: Becoming a Functional C++ Programmer 87
Chapter 6: Creating Functions 89
Chapter 7: Storing Sequences in Arrays 105
Chapter 8: Taking a First Look at C++ Pointers 121
Chapter 9: Taking a Second Look at C++ Pointers 135
Chapter 10: The C++ Preprocessor 153
Part III: Introduction to Classes 167
Chapter 11: Examining Object-Oriented Programming 169
Chapter 12: Adding Class to C++ 175
Chapter 13: Point and Stare at Objects 191
Chapter 14: Protecting Members: Do Not Disturb 207
Chapter 15: “Why Do You Build Me Up, Just to Tear Me Down, Baby?” 215
Chapter 16: Making Constructive Arguments 225
Chapter 17: The Copy/Move Constructor 247
Chapter 18: Static Members: Can Fabric Softener Help? 261
Part IV: Inheritance 271
Chapter 19: Inheriting a Class 273
Chapter 20: Examining Virtual Member Functions: Are They for Real? 281
Chapter 21: Factoring Classes 291
Part V: Security 301
Chapter 22: A New Assignment Operator, Should You Decide to Accept It 303
Chapter 23: Using Stream I/O 315
Chapter 24: Handling Errors — Exceptions 337
Chapter 25: Inheriting Multiple Inheritance 347
Chapter 26: Tempting C++ Templates 359
Chapter 27: Standardizing on the Standard Template Library 369
Chapter 28: Writing Hacker-Proof Code 381
Part VI: The Part of Tens 407
Chapter 29: Ten Ways to Avoid Adding Bugs to Your Program 409
Chapter 30: Ten Ways to Protect Your Programs from Hackers 417
Most Helpful Customer Reviews
C++ for Dummies is a needed volume. The impact of the 2009 Standard is covered in the book and is the prime reason I purchased the book. The standard offers some subtle changes. Various compilers purporting to provide 2009 Standard support do not supply enough information. This book does provide the information and ways to verify the Standard support is present. The rest of the book is C++ (not Candand as the request for review suggests) through and through with the author's insight into classes, inheritance and streaming I/O. I have not as yet tried all the examples on the CD but with the book and the compiler to use, I should have no problems. It has been a long time since the 5th edition came out and this book is a needed update. Hopefully the author will update his website to alert others to the presence of this book (the 6th edition). For the money, this book is a great value.
I give it a C++
Also have the pdf but if you are going to be reading this title on a nook ereader (like me) u may want to go ahead and buy it because the pdf foes not format well on the nook!
I didn't buy this book, but I did find a free pdf version of this entire book online. This is an excellent book for the beginners who want to go into programming.
This book does not give many examples and does a poor job showing getters and setters. If I could I would return the ebook and purchased a Deitel c++ book.
Its good but its sppose to have a CD that come whith it
I'm in my repeat attempt at passing a class that teaches pseudo code and as a hidden benefit, we have to learn to translate it to C++. The text book doesn't teach C++, it teaches the aforementioned. This book came in so handy when I had to learn how to declare variables, how to start a program, how to assign values and so on. Yes, I'm at the basics. Never mind the title, the book is good! C
Both of my children enjoy this book and refer back to it when needed.
I wouldn't recommend reading this book if you've never programmed before or you are searching for a primary readng book for learning C++. It's a good reference for C++ programmers but it is probably to hard for a beginner of programming to start with. Recommendeded ONLY if you need a reference. If you are new to programming, look up 'beginning programming' in the 'For dummies' series and Visual Basic 6 in the for dummies series as well, those are better starter books.
This book covers the material it claims but there are multiple errors in the programs written in the book. This lack of attention to detail is found in the often confusing and imprecisely written explanations. I would recommend this book as a casual reference or supplimentary text but not as a primary source of studying C++.
GOOD BOOK FOR BEGINNERS!!! BUY IT TO LEARN QUICK AND PAINLESSLY