Appendix D
876
.EXE file, and type the name of the program (with no extension). You can find out more about MS-DOS using the Windows help system.
Precompiled Header Files
You can speed up compilation dramatically by selecting Options from the Project menu, selecting the Compiler tab, and clicking on Use Precompiled Headers. In a short program most of the compile time is spent compiling the C++ header files such as iostream. Using the Precompiled Headers option causes these header files to be compiled only once, instead of each time you compile your program.
Closing and Opening Projects
When you’re done with a project, you can close it by selecting Close All from the File menu. To open a previously-saved project, select Open Project from the File menu, navigate to the appropriate .BPR file, and double-click it.
Adding a Header File to Your Project
Most serious C++ programs employ one or more user-written header files (in addition to many library header files, such as IOSTREAM and CONIO.H). Here’s how to create a header file.
Creating a New Header File
Select New... from the File menu, make sure the New tab is selected, and double-click the Text icon. You’ll see a code editor window with a file titled FILE1.TXT. Type in the text of your file and save it using Save As on the File menu, with an appropriate name, followed by the .H file extension. Save it in the same file as your source (.CPP) files. The new filename will appear on a tab next to the other files in the code editor window. You can switch from file to file by clicking the tabs.
Editing an Existing Header File
To open an existing header file, select Open from the File menu, and select Any File (*.*) from the Files of Type list. You can then select the header file from the list.
When you write the #include statement for the header file in your .CPP file, make sure you enclose the filename in quotes:
#include “myHeader.h”
The quotes tell the compiler to look for the header file in the same directory as your source files.
Borland C++Builder
877
Telling C++Builder the Header File’s Location
If you add a .H file, the compiler must know where to find it. If it’s in the same directory as your other files, you don’t need to do anything.
However, if your .H file is in a different directory, you’ll need to tell C++Builder where to find it. (This is true of the BORLACON.H file necessary for console-mode graphics, unless you copy it to your project file.) Go to Options on the Project menu and select the Directories/Conditionals tab. In the Directories section, click the button with the three dots on the right of the Include Path list. A Directories dialog box will appear.
In the bottom field of the Directories dialog box, type the complete pathname of the directory where the .H file is located. Click the Add button to place the path in the list of include paths. Then click OK twice more to close the dialog boxes.
Don’t try to add header files to the project with the Add to Project option in the Project menu.
Projects with Multiple Source Files
Real applications, and some of the example programs in this book, require multiple source (.CPP) files. Incidentally, in C++Builder, source files are often called units, a term specific to this product. In most C++ development environments, files are called files or modules.
Creating Additional Source Files
You make additional .CPP files the same way you make header files: Select File/New and double-click the Text icon in the New dialog box. Type in the source code, and use Save As to save the file. When using Save As, be sure to select C++Builder Unit (.CPP) from the Save File as Type list. This will automatically supply the .CPP extension, so all you need to type is the name. If you fail to do this, and simply type the .CPP after the name, the file won’t be recognized as a C++Builder unit.
Adding Existing Source Files to Your Project
You may have created a new source file as just described, or one may already exist, such as BORLACON.CPP, which is used for Console Graphics Lite programs. To add a source file to the project, select Add to Project from the Project menu, navigate to the appropriate directory (if necessary), and select the filename from the list. Then click Open. That tells C++Builder it’s part of the project.
Multiple source files are displayed with tabs in the Edit window (if they’re in full-size windows), so you can quickly switch from one file to another. You can open and close these files individually, so they don’t all need to be on the screen at the same time.
Appendix D
878
The Project Manager
You can see what source files are part of the project by selecting Project Manager from the View menu. You’ll see a diagram of file relationships, similar to the one shown in the Windows Explorer. Clicking the plus sign next to the project icon will display all the project’s source files. The file you just added to the project should be among them.
If you right-click a file in the Project Manager the context menu will show you choices that include Open, Save, Save As, and Compile. This is a handy way to perform these tasks on individual source files.
In a multifile program you can compile individual files separately by selecting Compile Unit from the Project menu. You can compile and link all the source files by selecting Make from the Project menu. This will cause only those source files that have been changed since the previous compile to be recompiled.
Console Graphics Lite Programs
Here’s how to build programs that use the Console Graphics Lite package. This includes such programs as CIRCSTRC from Chapter 5, “Functions,” and CIRCLES in Chapter 6, “Objects and Classes.”
•Create a new project as described earlier, using the program name as the project name, but with the .BPR extension.
•In the source file, change #include<msoftcon.h> to #include<borlacon.h>.
•Copy (don’t just move) BORLACON.H and BORLACON.CPP into your project file. (Or tell the compiler where the header file is located, as described earlier.)
•Add the source file BORLACON.CPP to your project by following the instructions in the section earlier in this Appendix titled “Adding Existing Source Files to your Project.”
•To keep the display on the screen, insert the line getch(); just before the return statement at the end of main().
•To support getch(), insert the line #include <conio.h> at the beginning of your program.
Now you can compile, link, and execute Console Graphics Lite programs just like other programs.
Debugging
In Chapter 3, “Loops and Decisions,” we suggest using a debugger to provide insight into how loops work. Here’s how to do that with C++Builder. These same steps can help you debug
Appendix D
880
Run to Cursor from the Run menu. However, there are times when you want to be able to stop the program in multiple locations. For example, you might want to stop it after an if and also after the corresponding else. Breakpoints solve this problem because you can insert as many as you need. (They also have advanced features we won’t describe here.)
Inserting a breakpoint in your listing is easy. Look at your program listing in the Edit window. You’ll see a dot in the left margin opposite each executable program line. Simply left-click the dot where you want to insert the breakpoint. You’ll see a red circle appear in the left margin, and the program line will be highlighted. Now whenever you run your program at full speed (by selecting Run from the Run menu, for example) it will stop at the breakpoint. You can then examine variables, single-step through the code, or run to another breakpoint.
To remove the breakpoint, left-click it again. It will vanish.
There are many other features of the debugger, but what we’ve described here will get you started.
Appendix E
882
It’s nice to be able to enliven example programs with graphics, so we’ve included some graphics-based examples in this book. Standard C++ does not include graphics specifications, but it certainly doesn’t prohibit graphics, and Windows supports various kinds of graphics.
Microsoft Visual C++ and Borland C++ use different library functions for graphics, and neither does everything we want it to. To avoid having two versions of each graphics example, and to gain extra capability, we use our own set of graphics functions, called Console Graphics Lite. These functions are translated into a Microsoft version or a Borland version, depending on which files you use to build your program: MSOFTCON.H and MSOFTCON.CPP for Microsoft, or BORLACON.H and BORLACON.CPP for Borland. (It’s possible that the files used for the Microsoft compiler will work with other compilers as well.)
The files for Console Graphics Lite can be downloaded from the publisher’s Web site. If you downloaded the source files for the example programs, you should have these graphics files already. If not, the Introduction has instructions for downloading. Listings for these files appear at the end of this Appendix.
Our graphics routines use console graphics. The console is a character-mode screen, typically arranged with 80 columns and 25 rows. Most of the non-graphics example programs in this book write text to the console window. A console program can run in its own window within Windows, or as a standalone MS-DOS program.
In console graphics, rectangles, circles, and so forth are made up of characters (such as the letter ‘X’ or a small character-size block) rather than pixels. The results are crude but work fine as demonstration programs.
Using the Console Graphics Lite Routines
To build an example program that uses graphics, you must add several steps to the normal build procedure. These are as follows:
•Include the appropriate header file (MSOFTCON.H or BORLACON.H) in the source (.CPP) file for the example program.
•Add the appropriate source file (MSOFTCON.CPP or BORLACON.CPP) to your project, so it can be linked with the example program.
•Make sure the compiler can find the appropriate header file and source file.
The header files contain declarations for the Console Graphics Lite functions. The source files contain the definitions for these functions. You need to compile the appropriate source file and link the resulting .OBJ file with the rest of your program. This happens automatically during the build process if you add the source file to your project.
Appendix E
884
The set_color() function can use either one or two arguments. The first sets the foreground color of characters displayed subsequently, and the second (if present) sets the background color of the character. Usually you want to keep the background black.
set_color(cRED); |
//sets foreground to red |
set_color(cWHITE, cBLUE); |
//foreground white, background blue |
Here is a list of the color constants that can be used for either foreground or background.
Color Constants for set_color()
cBLACK |
cDARK_GRAY |
cDARK_BLUE |
cBLUE |
cDARK_GREEN |
cGREEN |
cDARK_CYAN |
cCYAN |
cDARK_RED |
cRED |
cDARK_MAGENTA |
cMAGENTA |
cBROWN |
cYELLOW |
cLIGHT_GRAY |
cWHITE |
The functions beginning with draw_ create shapes or lines using a special character called the fill character. This character is set to a solid block by default, but can be modified using the set_fill_style() function. Besides the solid block, you can use uppercase ‘X’ or ‘O’ characters, or one of three shaded block characters. Here is a list of the fill constants:
Fill Constants for set_fill_style()
SOLID_FILL |
LIGHT_FILL |
X_FILL |
MEDIUM_FILL |
O_FILL |
DARK_FILL |
The wait() function takes an argument in milliseconds, and pauses for that amount of time.
wait(3000); //pauses for 3 seconds
The other functions are largely self-explanatory. Their operation can be seen in those examples that use graphics.
Implementations of the Console Graphics Lite
Functions
These routines used for Console Graphics Lite aren’t object-oriented, and could have been written in C instead of C++. Thus there’s no real reason to study them, unless you’re interested in a quick-and-dirty approach to graphics operations such as drawing lines and circles. The