C++ Builder 3 and 4 Standard Edition
Overview
C++ Builder 3 and 4 Standard Edition are inexpensive C++ environments,
with university editions available at a further discount. Some packages
actually contain three compilers: C++ Builder 3 or 4 (discussed here),
Borland C++ 5.0, and Turbo C++ 4.5.
C++ Builder is principally an environment for rapid building of Windows
applications. You can think of it as a C++ equivalent of Visual Basic. The
environment is not designed to teach students how to program. However,
under the hood, C++ Builder has an excellent C++ compiler (Borland C++
5.30 or 5.40).
Installation
When installing from the CD ROM, you can choose Compact Install to
minimize the install footprint. (Minor drawback: some help files are then
left on the CD ROM.) There seems to be no way to avoid the installation of
the database engine (which is useless for introductory programming).
Compiling Console Applications
A console application is an application that reads input from
cin and writes output to cout, such as hello.cppfrom
Chapter 1. Here are the instructions to compile and run a console
application with C++ Builder.
- Start C++ Builder
- Select the File|New... menu option
- Click on the Console Wizard icon
- Click on one of the Window Type radio buttons to set the
application type to Console
- Check that the Execution Type radio button is set to EXE
- Click on the Finish button. Now a default project has been
created.
- Select the File|Save as menu option
- In the resulting dialog, select the directory in which you want to
store the project (such as c:\myhomework or c:\ccc2book\examples\ch1).
Then give a name to your project. If you start a new project and you
don't yet have a C++ source file, then simply choose some reasonable
name, for example Homework1. If you already have an existing
C++ source file in the same directory (such as an example file from the
code accompanying the book), be sure to give the project a different
name. For example, if you already have a hello.cpp, call the
project HelloProj. Finally, click on the Save button.
- You now have a window with a tab, containing a default C++ file with
the same name as the project, such as Homework1.cpp or HelloProj.cpp.
- If you are starting a new project, just write your program in the
space provided. Leave the strange #pragma and #include
directives in place, and start adding your code below the last #pragma.
- If you want to add exisiting files to the project (such as one of the
example files from the code accompanying the book), then select the Project|Add
to project... menu option . Select one or more files (such as hello.cpp)
and click on the Open button. Now you have to do something very
strange. Go back to the main C++ file (such as HelloProj.cpp)
and locate the main function. You don't want it--one of the
files that you included already has a main function. Do
not simply erase it. If you do, you get bizarre error messages from
C++ Builder. Instead, place an #if 0 and #endif
around it, like this:
#pragma hdrstop
#include <condefs.h>
//---------------------------------------------------------------------------
USEUNIT("hello.cpp");
//---------------------------------------------------------------------------
#pragma argsused
#if 0
int main(int argc, char **argv)
{
return 0;
}
#endif
- If you compile one of the example programs from the code
accompanying the book, erase the line #include "ccc_ansi.h"
. The Borland compiler does not need the CCC ANSI C++ compatibility
library.
- If your program uses Boolean operators and or not, you
must replace them with the old-style && || ! symbols.
The Borland compiler does not know about the and or not
keywords. (Unfortunately, the compiler violates the ANSI/ISO standard in
this regard.)
- Locate the end of your main function. Before main
returns, add a line
cout << "Hit ENTER to quit: "; cin.get();
This is
necessary to make the program stop before
main ends. The C++ Builder environment immediately closes the
console window when main returns, and you can't admire your
output.
- Select the Project|Make menu option. Fix any compiler errors.
- Once the program compiles error-free, select the Run|Run menu
option. The program runs. At the end, the program stops at the cin.get()
instruction that you inserted. Hit the ENTER key for the program to
quit.
- To start the debugger, select Run|Trace Into.
Compiling Windows Applications
A Windows application is an application that draws graphical
shapes on a window, such as circle.cppfrom Chapter 3. Here are
the instructions to compile and run a Windows application with C++
Builder.
- Start C++ Builder
- Select the File|New... menu option
- Click on the Console Wizard icon
- Click on one of the Window Type radio buttons to set the
application type to Window (GUI)
- Check that the Execution Type radio button is set to EXE
- Click on the Finish button. Now a default project has been
created.
- Select the File|Save as menu option
- In the resulting dialog, select the directory in which you want to
store the project (such as c:\myhomework or c:\ccc2book\examples\ch1).
Then give a name to your project. If you start a new project and you
don't yet have a C++ source file, then simply choose some reasonable
name, for example Homework1. If you already have an existing
C++ source file (such as an example file) in the same directory, be sure
to give the project a different name. For example, if you
already have a hello.cpp, call the project HelloProj.
Finally, click on the Save button.
- You now have a window with a tab, containing a default C++ file with
the same name as the project, such as Homework1.cpp or HelloProj.cpp.
If you are starting a new project, just start writing your program in
the space provided. Just leave the strange #pragma and #include
directives in place, and start adding your code below the last #pragma.
- If you want to add exisiting files to the project (such as one of the
example files from the code accompanying the book), then select the Project|Add
to project... menu option . Select one or more files (such as hello.cpp)
and click on the Open button. Now you have to do something very
strange. Go back to the main C++ file (such as HelloProj.cpp)
and locate the WinMain function. You don't want it--the ccc_win.cpp
file already has a main function. Do not simply erase it.
If you do, you get bizarre error messages from C++ Builder. Instead,
place an #if 0 and #endif around it, like this:
#include <windows.h>
#pragma hdrstop
#include <condefs.h>
//---------------------------------------------------------------------------
USEUNIT("circle.cpp");
//---------------------------------------------------------------------------
#pragma argsused
#if 0
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
return 0;
}
#endif
- If your program uses Boolean operators and or not, you
must replace them with the old-style && || ! symbols.
The Borland compiler does not know about the and or not
keywords. (Unfortunately, the compiler violates the ANSI/ISO standard in
this regard.)
- Select the Project|Options menu option
- In the resulting dialog, click on the Directories/Conditionals
tab
- In the Include path text field, append the path to the CCC
source files, such as ;c:\ccc2book\cccfiles.
- Click on the OK button
- Select the Project|Make menu option. Fix any compiler errors.
- Once the program compiles error-free, select the Run|Run menu
option. The program runs. To terminate the program, close the graphics
window.
- To start the debugger, place the cursor into the first line of the
main function of your application and select Run|Run to
Cursor.
Known Problems
The C++ Builder environment, like so many others, forces you to create a
project for every program that you want to compile, even if your
program contains only a single source file. However, unlike other
environments, C++ Builder insist that the project and the main C++ source
file have the same name. And the environment constructs a default
C++ file with that name whenever you make a new project. Therefore, it is
very important that you give a different name than any existing
file name if you want to make a project to compile an existing file (such
as one of the example files).
Be careful when adding an existing program to a project. Your program
will already have a main or a WinMain function. The
environment adds a main or WinMain function into the
main C++ source file. Since your program has a main function in
one of the files that you added, you now have two main
functions. Eliminating the main or WinMain function in
the main C++ source file does not work. You get bizarre compiler
errors. Instead, you have to comment out the dummy function with a set of
#if 0...#endif preprocessor directives.
This environment is one of those unpleasant environments that hasn't
figured out how to make a console window stay on the screen after the
program terminates. For that reason, you must include some line at the end
of every program that makes the program wait for a keystroke, for example
int main()
{ cout << "Hello, World\n";
cin.get();
}
Then the program stops at the end, and you must hit the ENTER key to
terminate the program and return to the environment.
For console applications, you do not need the CCC ANSI C++ compatibility
library. The Borland 5.30 compiler appears to be almost compatible with
the ANSI standard. The major problem is that the compiler doesn't know
about the and or not keywords. Remedy: replace them with &&
|| !.
There is one other Borland 5.30/5.40 bug that makes the file
database.cpp in chapter 10 not compile. Remedy: change
void read_employee(Employee& e, fstream& fs)
{ string line;
getline(fs, line);
. . .
}
to
void read_employee(Employee& e, fstream& fs)
{ string line;
istream& ifs = fs;
getline(ifs, line);
. . .
}