Home Catalog Evaluate Download

Sams Teach Yourself C++ In 21 Days

by Jesse Liberty

One of the best selling C++ programming textbooks is Sams Teach Yourself C++ In 21 Days by Jesse Liberty and published by Sams Publishing. This book is currently in its fourth edition. The home page for this book can be found at:


You can go to the Amazon on-line bookstore (www.amazon.com) to observe that customers review this book favorably. You might also be able to locate this book in your local library (be aware that many libraries will purchase a book when someone requests one they don't already have).

Sams Publishing makes available on their web site the source code that is shown in this book. Just go to the web page listed above and then search on the keyword "Liberty". On the page for this particular book (Liberty has written others) you will find a hyperlink labeled "Downloads" which allows you to obtain the file named "source.zip" which contains all of the source code demonstrated in the book. After clicking on the source.zip hyperlink choose "Save this file to disk" and then specify a destination folder such as "C:\Sams" or "C:\Computer Science Lab\CppIde\Sams".

Because this .ZIP file holds only the .CPP and .H files, you will need to prepare project folders for each program before you can compile this code using any commercial IDE such as Microsoft's Visual C++ or Metrowerks CodeWarrior. This is a minor nuisance so I have already prepared the project files (.MAK makefiles and .PRJ files) that allow you to compile, execute, and debug these programs from my CPPIDE integrated development environment. You will find these project files in the folder named "Sams Teach Yourself C++" on my Computer Science Lab CD-ROM.

Note that because the Computer Science Lab CD-ROM is an "auto-run" CD, my normal installation program will automatically display a Setup window a few seconds after you insert the CD-ROM. You have presumably already used this Setup program to install CPPIDE so you don't need to click on "Install". Instead, click on "Explore CD". This causes a normal Windows Explorer window to open, showing you the contents of the CD-ROM. Browse (navigate) to the folder named "Sams Teach Yourself C++". Inside that folder you will see the following contents:

Sams.htm                 [this is the file you are now reading]

Copy all of this content to the same destination folder where you unpacked the contents of source.zip. Then double-click on SamsConvert.exe which will initiate the installation program that installs CPPIDE project folders for each of the example programs.

NOTE: It is important that you unpack the source.zip file BEFORE you initiate the SamsConvert.exe executable.

Once this process is complete you will be able to navigate into the destination folder you selected and observe the following folders:


If you proceed into the Day01 folder you will find the following folders:


If you proceed into the List0101 folder you will find the following files:


The .CPP file is the original source code file that came from source.zip while the .MAK and .PRJ files originated from SamsConvert.exe.

To compile any of the Sams example programs using CPPIDE, you simply need to open the corresponding .PRJ file and then click on the "Compile" button found on CPPIDE's toolbar.

There are a total of 179 example programs in all the chapters of the Sams book. These are all very short console-mode programs.

Some of these programs generate errors when they are compiled from within CPPIDE. Sometimes this is intentional, as the author wishes to demonstrate a particular aspect of the C++ language. Sometimes it is unintentional as the program has an obvious typo. But there are a few programs that won't compile with CPPIDE because CPPIDE employs the Gnu C++ compiler while the author (Jesse Liberty) developed his programs using Microsoft's Visual C++ compiler. The difference is that the Gnu C++ compiler adheres very closely to the official C++ language standard whereas Microsoft has always been willing to invent their own standards. The programs that will not compile with the Gnu C++ compiler are discussed below under "Book Errata".

Some of the example programs (for example, 4eList1306.cpp and 4eList1307.cpp in the Day13 chapter) generate quite a few lines of text. If you are employing CPPIDE under Windows NT/2000/XP then the console window will be equipped with scroll bars which you can employ to read all of the text that is output to the window. But if you are running CPPIDE under Windows 95/98/ME then the console window lacks scroll bars (blame Microsoft) and you will only be able to observe the last page of text since the previous lines have scrolled off the top. The solution here is to run these programs from the console window (Start/Programs/MS-DOS Prompt) rather than from CPPIDE and to then pipe their output to "more", as is shown below:

<program name> | more

"More" is a paginator supplied by Microsoft which will halt the program after every page of text and wait for you to hit a key on the keyboard indicating you are ready for the next page.

A few of the example programs (specifically, 4eList1719.cpp and 4eList1720.cpp in the Day17 chapter) require command line parameters. To initiate this program from the command line you need to type more than just the program's name since you also want to give the program some additional arguments. When you initiate a console-mode program using the "Execute" button seen on CPPIDE's toolbar, you don't get an opportunity to provide these additional command line arguments. However CPPIDE does offer this ability. Before initiating a program that requires command line arguments, open the Options dialog (View/Options) and then enter the command line arguments in the EDIT control labeled "command line arguments". Note that these command line arguments will continue to be sent to whatever programs you initiate with CPPIDE until you remove them.

The 4eList1704.cpp program in the Day17 chapter runs until it receives the EOF character. To generate this character from the keyboard you type CTRL+Z.

The 4eList0101.cpp program in the Day01 chapter is identical to the 4eList0201.cpp program in the Day02 chapter. Both programs require the operator to click on the console-mode window (in order to give it keyboard focus) and then to type an integer (which will not be displayed) before the program will terminate.

Book Errata

In the Day01 chapter, the 4eList0102.cpp file is missing its closing curly brace and hence won't compile.

In the Day02 chapter, the 4eList0202.cpp file is missing the initial double quote character at the start of "writes a new line to the screen" and hence won't compile.

In the Day06 chapter, the 4eList0605.cpp file violates a const declaration and hence won't compile (this is intentional in order to demonstrate the const concept).

Quite a few of the example programs define a Rectangle class (specifically: 4eList0609.cpp, 4eList1001.cpp, 4eList1002.cpp, 4eList1003.cpp, 4eList1407.cpp, 4eList1408.cpp, 4eList1409.cpp). None of these programs will compile unless all occurrences of "Rectangle" are replaced with something like "Rectangle2". I can't explain this situation, it seems as though the Gnu compiler is treating the identifier "rectangle" as a reserved keyword.

In the Day07 chapter, the 4eList0703.cpp and 4eList0704.cpp files won't compile unless all occurrences of "small" are replaced with something like "small2". This one I can explain: in the <rpcndr.h> header file the identifier "small" is used as a macro name for the char data type.

In the Day07 chapter, the 4eList0708.cpp file won't compile unless you change the statement:

    cout << "Looping!  ";


    std::cout << "Looping!  ";

In the Day07 chapter, the 4eList0710.cpp file won't compile unless you change the statement:

    std::cout << "i: " << i << " j: " << j << endl;


    std::cout << "i: " << i << " j: " << j << std::endl;

In the 4eList1013.cpp and 4eList1718.cpp files you should change:

    #include <iostream.h>


    #include <iostream>

or you will get an warning from the compiler that you are using a "deprecated feature".

In the Day10 chapter in the 4eList1016.cpp file is not supposed to compile because it attempts to assign an int to a class variable. Nonetheless it has a typo. You need to change:

    #using namespace std;


    using namespace std;

In the Day15 chapter the 4eList1507.cpp file will not compile unless you change:

    const MaxArray = 5;


    const int MaxArray = 5;

Furthermore, the 4eList1510.cpp and 4eList1511.cpp programs will not compile unless you change the statements such as:

    case 1: pFunc = Mammal::Speak; break;


    case 1: pFunc = &Mammal::Speak; break;

The following programs will not compile due to differences between the Gnu C++ compiler and the Microsoft C++ compiler: 4eList1602.cpp, 4eList1604.cpp, 4eList1605.cpp, 4eList1606.cpp, 4eList1607.cpp, 4eList1904.cpp, 4eList1905.cpp, 4eList1906.cpp, and 4eList1907.cpp.

The 4eList2105.cpp and 4eList2106.cpp programs in the Day21 chapter don't compile because they try to define TRUE and FALSE which are already defined whenever you #include <iostream>. To get these programs to compile you just need to comment out these statements.

Return to the Catalog of Programs