appendix l: installing and using borland c++ builder 6 · 2006. 3. 23. · appendix l: installing...

22
1 Appendix L: Installing and Using Borland C++ Builder 6 When purchased new, this book comes with a CD containing Borland C++ Builder 6. This appendix guides you through the steps of installing C++ Builder, and using it to create C++ programs. Installing Borland C++ Builder 6 To install Borland C++ Builder 6 you must obtain a serial number and authorization key from the Borland Web site, and then install the software from the accompanying CD. The following sections lead you through the process. Obtaining a Serial Number and Authorization Key When you install C++ Builder, you will be asked to enter a serial number and an authori- zation key. Borland will email the serial number and authorization key to you for free after you visit the following Web site and set up an account: http://www.borland.com/downloads/download_cbuilder.html Near the bottom of the Web page you will see a table titled Keys Only (If you have a CD) . Under the Name column you will see a link titled Personal . Click the Personal link. Follow the instructions you see next to set up a login account. Once you have set up your login account, the serial number and authorization key will be sent to you in an email message. Save or print the email message so you can refer to it during the installation process. Installing the Borland C++ Builder 6 Software 1. Run Install.exe from the accompanying CD. The window shown in Figure L-1 will appear. Click the button that reads C++ Builder 6 .

Upload: others

Post on 30-Jan-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

  • 1

    Appendix L: Installing and Using Borland C++ Builder 6

    When purchased new, this book comes with a CD containing Borland C++ Builder 6. Thisappendix guides you through the steps of installing C++ Builder, and using it to create C++programs.

    Installing Borland C++ Builder 6

    To install Borland C++ Builder 6 you must obtain a serial number and authorization keyfrom the Borland Web site, and then install the software from the accompanying CD. Thefollowing sections lead you through the process.

    Obtaining a Serial Number and Authorization Key

    When you install C++ Builder, you will be asked to enter a serial number and an authori-zation key. Borland will email the serial number and authorization key to you for freeafter you visit the following Web site and set up an account:

    http://www.borland.com/downloads/download_cbuilder.html

    Near the bottom of the Web page you will see a table titled

    Keys Only (If you have a CD)

    .Under the Name column you will see a link titled

    Personal

    . Click the

    Personal

    link. Followthe instructions you see next to set up a login account. Once you have set up your loginaccount, the serial number and authorization key will be sent to you in an email message.Save or print the email message so you can refer to it during the installation process.

    Installing the Borland C++ Builder 6 Software

    1. Run Install.exe from the accompanying CD. The window shown in Figure L-1 will appear. Click the button that reads

    C++ Builder 6

    .

  • 2

    Appendix L: Installing and Using Borland C++ Builder 6

    2. The installation wizard will begin, as shown in Figure L-2. Click the

    Next

    button to continue the installation.

    3. The next window, shown in Figure L-3, prompts you to enter the serial number and authorization key. This information is in the email message you received when you set up your login account on the Borland Web site. Enter the serial number and authori-zation key and then click the

    Next

    button.

    Figure L-1

    Figure L-2

    Click Here

  • Appendix L: Installing and Using Borland C++ Builder 6

    3

    4. The next screen, shown in Figure L-4, shows the license agreement. Read the license agreement, select

    I accept the terms in the license agreement

    , and click the

    Next

    but-ton to continue.

    Figure L-3

    Figure L-4

  • 4

    Appendix L: Installing and Using Borland C++ Builder 6

    5. The next window, shown in Figure L-5, shows miscellaneous installation notes. These notes describe various installation issues that exist for some users, and describe the minimum system requirements. Briefly look through these notes for any issues that might affect your installation, and then click the

    Next

    button to continue.

    6. The next window, shown in Figure L-6, prompts you to select the type of installation you wish to perform. It is recommended that you select

    Typical

    , and then click the

    Next

    button.

    Figure L-5

    Figure L-6

  • Appendix L: Installing and Using Borland C++ Builder 6

    5

    7. The next window, shown in Figure L-7, prompts you to select the version of Office controls to register. Click the

    Next

    button to accept the default selection.

    8. If you see the window shown in Figure L-8, it indicates that another C++ development environment is probably installed on your computer (possibly an earlier version of C++ Builder). If you wish to automatically associate these file types with C++ Builder 6, leave all the boxes checked. (This is recommended if C++ Builder 6 is going to be your primary C++ development environment.) If you do not wish to associate these file types with C++ Builder 6, leave the boxes unchecked. Click the Next button to continue.

    Figure L-7

    Figure L-8

  • 6

    Appendix L: Installing and Using Borland C++ Builder 6

    9. The next window, shown in Figure L-9, allows you to select a destination folder for the C++ Builder files. Either use the default locations or select other folders for the files. Click the

    Next

    button to continue.

    10. The next window, shown in Figure L-10, asks whether you wish to copy the installa-tion database to your hard drive. Leave the box checked and click the

    Next

    button.

    Figure L-9

    Figure L-10

  • Appendix L: Installing and Using Borland C++ Builder 6

    7

    11. On the next window, shown in Figure L-11, click the

    Install

    button to begin the instal-lation. A window similar to Figure L-12 will appear with changing messages while the software is being installed.

    12. When the installation finishes, the window shown in Figure L-13 will appear. Click the

    Finish

    button. You will be prompted to restart your computer for the installation to be complete.

    Figure L-11

    Figure L-12

  • 8

    Appendix L: Installing and Using Borland C++ Builder 6

    13. The last step is to register your software. After restarting your computer, launch Bor-land C++ Builder 6. You will see a registration window prompting you to select your method of registration (online, by phone, or at a later time). Select your preferred method of registration and follow the instructions.

    Using Borland C++ Builder 6

    The remainder of this appendix serves as a quick reference for performing the followingoperations using the Borland C++ Builder integrated development environment (IDE):

    Starting a new project and entering code

    Saving a project to disk

    Compiling and executing a project

    Pausing a program's output

    Closing a project

    Opening an existing project

    Creating a multi-file project

    Adding example source files to a project

    Determining where data files created by a program are located

    Figure L-13

  • Appendix L: Installing and Using Borland C++ Builder 6

    9

    Starting a New Project

    The first step in creating a program with Borland C++ Builder is to start a

    project

    . Aproject is a group of one or more files that make up a software application. (Even if yourprogram consists of no more than a single source code file, it still must belong to aproject.) To start a project:

    1. Launch Borland C++ Builder. The IDE windows open on the desktop, similar to what is shown in Figure L-14.

    2. When C++ Builder starts, it automatically begins an empty project. This project is usually named Project1. This project is not set up properly for the programs in this book, however, so you will begin a new project. Click

    File

    on the menu bar. On the File menu, click

    New

    , then click

    Other...

    You see the New Items dialog box shown in Figure L-15. (Make sure the

    New

    tab at the top of the dialog box is selected.)

    Figure L-14

  • 10

    Appendix L: Installing and Using Borland C++ Builder 6

    3. All of the programs in this book are console programs, so click

    Console Wizard,

    then click the

    OK

    button. The dialog box shown in Figure L-16 is displayed.

    4. Make sure

    C++

    and

    Console Application

    are selected. No other options should be selected. Click the

    OK

    button. A dialog box appears asking “Save changes to Project1?” (The name you see may be different from Project1.) Because the default project is empty, click the

    No

    button. Your screen now looks similar to Figure L-17.

    Figure L-15

    Figure L-16

  • Appendix L: Installing and Using Borland C++ Builder 6

    11

    5. The window that is currently titled

    Unit1.cpp

    is the text editor. C++ Builder auto-matically creates a program “skeleton” for you. The skeleton contains some prepro-cessor directives and an empty function

    main

    . You can modify the code skeleton, or erase it and write your own program from scratch. For example, Figure L-18 shows a simple program that has been written in the editor after the default program skeleton has been erased.

    Figure L-17

  • 12

    Appendix L: Installing and Using Borland C++ Builder 6

    Saving a Project to Disk

    It is important to periodically save your work. To save your project for the first time, click

    File

    on the menu bar, and then click

    Save Project As…

    on the File menu. A Save As dialogbox appears for the C++ source file. The source file’s default name is Unit1.cpp. Click the

    OK

    button if you want to keep this name. If you wish to save the source file under a differ-ent name, enter the new name, and click the

    OK

    button. (Make sure you use the .cppextension if you change the name of the file.)

    Next, a Save As dialog box appears for the project file. A default name, such asProject2.bpr, will appear as the project file’s name. Click the

    OK

    button if you want tokeep this name. If you wish to save the project file under a different name, enter the newname, and click the

    OK

    button. (Make sure you use the .bpr extension if you change thename of the file.)

    After you have saved the project the first time, you may save it subsequent times by click-ing

    File

    on the menu bar, then clicking

    Save All

    .

    Figure L-18

  • Appendix L: Installing and Using Borland C++ Builder 6

    13

    Compiling and Executing

    Once you have entered a program’s source code, you may compile and execute it by any ofthe following methods:

    by clicking the button

    by pressing

    F9

    by clicking

    Run

    on the menu bar, and then clicking

    Run.

    A window at the bottom of the editor pane appears if errors are found in your program.Double click an error message in the window, and the editor will highlight the line of codewhere the error was encountered.

    For example, look at the program in Figure L-19. The

    cout

    statement is missing a semico-lon. By double-clicking the error message, the text cursor is positioned on the line with theerror. (Actually, in this case the cursor appears on the line after the statement with themissing semicolon. The compiler did not detect that the semicolon was missing until itencountered the beginning of the next line. Finding errors is not always straightforward.)

    If the program compiles successfully, a console window appears and the program runs.This is illustrated in Figure L-20.

    Pausing a Program's Output

    Notice that the program shown in the previous example has a

    cin.get()

    statement infunction

    main

    . This statement causes the program to pause until the user presses the Enterkey. The statement is in the program because the console window automatically closes assoon as the program is finished. If the program did not have the

    cin.get()

    statement, itwould quickly flash the message “This is a simple program” on the screen, and the consolewindow would suddenly close.

    Figure L-19

  • 14

    Appendix L: Installing and Using Borland C++ Builder 6

    Sometimes a single

    cin.get()

    statement will not pause the screen. (This is because a pre-vous

    cin

    statement has left a newline character in the keyboard buffer.) A better way ofpausing the screen is to include the

    conio.h

    header file and use the

    getch()

    function atthe end of the program. This is demonstrated in Program L-1.

    Closing a Project

    To close the project that you currently have open, click

    File

    on the menu bar, and thenclick

    Close or Close All.

    Opening an Existing Project

    To open an existing project, click File on the menu bar, and then click on Open Project…Use the resulting dialog box to browse to the location of your project file. When you havelocated your project file, double-click it. The project will be opened with its source codefile(s) displayed in the text editor.

    Creating a Multi-File Project

    Many of your programs will consist of more than one file. For example, suppose yourinstructor has asked you to write a simple class named Pet. An instance of the class willhold the name of your pet. You decide to write the following specification and implemen-tation files.

    Figure L-20

    Program L-1

    1 #include 2 #include // Needed for the getch() function 3 using namespace std; 4 int main() 5 { 6 cout

  • Appendix L: Installing and Using Borland C++ Builder 6 15

    Contents of Pet.h

    1 // Pet class specification file 2 #ifndef PET_H 3 #define PET_H 4 #include 5 using namespace std; 6 7 class Pet 8 { 9 private:10 string name; // To hold the pet's name11 12 public:13 // Constructor14 Pet(string);15 16 // Accessor function for name17 string getName() const;18 } ;19 20 #endif

    Contents of Pet.cpp

    1 // Pet class implementation file 2 #include 3 #include "Pet.h" 4 using namespace std; 5 6 // Constructor 7 Pet::Pet(string str) 8 { 9 name = str;10 } 11 12 // getName member function13 string Pet::getName() const14 {15 return name;16 }

    To demonstrate the class, you decide to write the code in Program L-2.

    Program L-2

    1 #include 2 #include 3 #include "Pet.h" 4 using namespace std; 5 6 int main() 7 { 8 // Create an instance of the Pet class. 9 Pet myPet("Fido");

  • 16 Appendix L: Installing and Using Borland C++ Builder 6

    The following steps show you how to create all three files in the same project.

    1. Start a new project by following steps 1 through 5 under the section Starting a New Project.

    2. The text editor opens with a default file named Unit1.cpp. We will rename this file in a moment. Enter the C++ source code for Program L-2 into the text editor.

    3. After you have entered the code, you need to save the file under a more meaningful name. We will name the file PetDemo.cpp. Click File on the menu bar, then click Save As... A Save As dialog box appears. Browse to the location where you wish to save the file, and then enter PetDemo.cpp as the file name. Click the OK button. Your editor window will look similar to Figure L-21.

    4. Now we will write the source code for the Pet.h file. But first, we need to add a new, empty source file to the project. Click File on the menu bar. On the File menu, click New, then click Other... You see the New Items dialog box shown in Figure L-22. (Make sure the New tab at the top of the dialog box is selected.)

    10 11 // Display my pet's name. 12 cout

  • Appendix L: Installing and Using Borland C++ Builder 6 17

    5. In the New Items dialog box, select Header File and click the OK button. An empty file with a default name such as File1.h will be added to the project. We will rename the file in a moment. The editor window should now appear as shown in Figure L-23. Notice there are two tabs at the top of the window: one for PetDemo.cpp and another for File1.h. You can use these tabs to switch your view between the two files.

    Figure L-22

    Figure L-23

  • 18 Appendix L: Installing and Using Borland C++ Builder 6

    6. With the File1.h tab selected, enter the code for the Pet.h file. After you have entered the code, you need to save the file under the name Pet.h. Click File on the menu bar, then click Save As... A Save As dialog box appears. Browse to the location where you wish to save the file, and then enter Pet.h as the file name. Click the OK button. Your editor window will look similar to Figure L-24.

    7. Now we will write the source code for the Pet.cpp file. But first, we need to add a new, empty C++ source file to the project. Click File on the menu bar. On the File menu, click New, then click Other... You see the New Items dialog box. (Make sure the New tab at the top of the dialog box is selected.) Select Cpp File and click the OK button. An empty file with a default name such as File1.cpp will be added to the project. We will rename the file in a moment. Notice now there are three tabs at the top of the window: one for PetDemo.cpp, one for Pet.h, and one for File1.cpp.

    8. With the File1.cpp tab selected, enter the code for the Pet.cpp file. After you have entered the code, you need to save the file under the name Pet.cpp. Click File on the menu bar, then click Save As... A Save As dialog box appears. Browse to the location where you wish to save the file, and then enter Pet.cpp as the file name. Click the OK button. Your editor window will look similar to Figure L-25.

    Figure L-24

  • Appendix L: Installing and Using Borland C++ Builder 6 19

    9. All three files, PetDemo.cpp, Pet.h, and Pet.cpp, have been created in our project. Follow the instructions in the section Compiling and Executing to test the program.

    Adding Example Source Files to a Project

    If you wish to compile and execute one of the example source files that come with thisbook, you will have to create a project and add the file to the project. If the examplesource file is part of a multi-file program, you will have to add all of the files that are partof the program to the project. The following steps lead you through the process. (Thistutorial assumes you have copied the Appendix L example source code folder from theStudent CD to your hard drive. If you have not, do so before performing these steps.)

    1. Launch Borland C++ Builder. Click File on the menu bar. On the File menu, click New, then click Other... You see the New Items dialog box shown in Figure L-26. (Make sure the New tab at the top of the dialog box is selected.)

    Figure L-25

    NOTE: None of the example programs in this book have the getch() statement to pause the program's output. You will need to add the #inlcude directive and the getch() statement in order to pause the program's output.

  • 20 Appendix L: Installing and Using Borland C++ Builder 6

    2. Select Console Wizard, then click the OK button. The Console Wizard dialog box is then displayed. Make sure the following items are selected:

    • C++• Console Application • Specify project source

    Because we selected Specify project source, we must also select a source file to initiallyload into the project. Click the button showing the ellipsis ( ). The Specify ProjectSource dialog box should appear. Browse to the Appendix L example source code folderand select the file PrL-3.cpp and click the Open button. The Console Wizard dialog boxshould now appear similar to Figure L-27. Click the OK button.

    3. You should now see the program PrL-3.cpp open in the text editor. If this were the only file in the program, we could compile and execute it. This program, however, uses the Rectangle class, which is in the Rectangle.h and Rectangle.cpp files. Next we will add the Rectangle.cpp file to the project. Click Project on the menu bar. On the Project menu, click Add To Project… The Add to Project dialog box appears next. Use the dialog box to find the file Rectangle.cpp in the Appendix L

    Figure L-26

    Figure L-27

  • Appendix L: Installing and Using Borland C++ Builder 6 21

    example source code folder. When you locate the file, click it and then click the Open button. The file Rectangle.cpp is now a member of the project, and appears in the text editor window.

    4. The Rectangle.h file does not have to be added to the project because the PrL-3.cpp file has an #include directive that will cause Rectangle.h to be included just before compilation takes place. However, we might want to open the file in the text editor to examine it. To open the file, click File on the menu bar, then click Open. An Open dialog box appears. Use the dialog box to find the file Rectangle.h in the Appendix L example source code folder. When you locate the file, click it and then click the Open button. The file Rectangle.h now appears in the text editor window.

    5. Now you should save the project. Click File on the menu bar, and then click Save Project As… on the File menu. A Save As dialog box appears for the project file. Browse to the Appendix L example source code folder on your hard drive. A default name, such as Project2.bpr, will appear as the project file’s name. Click the OK but-ton if you want to keep this name. If you wish to save the project file under a different name, enter the new name, and click the OK button. (Make sure you use the .bpr extension if you change the name of the file.)

    6. Follow the instructions in the section Compiling and Executing to test the program.

    Where Data Files Are Created

    When you create a new project, you normally save all of the files associated with thatproject in the same folder. (See the Saving Your Project to Disk section.) This folder isknown as the project folder. For example, suppose we’ve created a project named Lab5,and saved it in the folder C:\MyProjects\Lab5. All of the C++ source files that you havecreated for this project, as well as the project file that ends with the extension .bpr, will bestored in that folder.

    It’s important to know the location of the project folder when writing code that opens filesfor input or output. When running a program from the C++ Builder IDE, the projectfolder is the default location for all data files. For example, suppose our Lab5 project cre-ates a file with the following code:

    ofstream outputFile("names.txt");

    Because we have not specified a path with the file name, the file will be created in theC:\MyProjects\Lab5 project folder. Similarly, if our program attempts to open an exist-ing file for reading, it will look in the project folder by default. For example, suppose ourLab5 program attempts to open a file with the following statement:

    ifstream inputFile("names.txt");

    Because we have not specified a path, the program will look for the file in the projectfolder, C:\MyProjects\Lab5. If we want to open a file in a location other than theproject folder, we must provide a path as well as a file name. For example, the followingstatement opens a file named Data.txt in the C:\Data folder.

    ofstream outputFile("C:\\Data\\Data.txt");

  • 22 Appendix L: Installing and Using Borland C++ Builder 6

    Notice that we’ve used two backslashes in the path where you normally use only one.Remember, in a literal string, the compiler interprets the backslash character as the begin-ning of an escape sequence. In order to represent a backslash as a character in a literalstring, you must use two backslashes. Here’s another example:

    ifstream inputFile("E:\\names.txt");

    This statement attempts to open the names.txt file in the root folder of drive E.

    /ColorImageDict > /JPEG2000ColorACSImageDict > /JPEG2000ColorImageDict > /AntiAliasGrayImages false /DownsampleGrayImages true /GrayImageDownsampleType /Bicubic /GrayImageResolution 300 /GrayImageDepth -1 /GrayImageDownsampleThreshold 1.50000 /EncodeGrayImages true /GrayImageFilter /DCTEncode /AutoFilterGrayImages true /GrayImageAutoFilterStrategy /JPEG /GrayACSImageDict > /GrayImageDict > /JPEG2000GrayACSImageDict > /JPEG2000GrayImageDict > /AntiAliasMonoImages false /DownsampleMonoImages true /MonoImageDownsampleType /Bicubic /MonoImageResolution 1200 /MonoImageDepth -1 /MonoImageDownsampleThreshold 1.50000 /EncodeMonoImages true /MonoImageFilter /CCITTFaxEncode /MonoImageDict > /AllowPSXObjects false /PDFX1aCheck false /PDFX3Check false /PDFXCompliantPDFOnly false /PDFXNoTrimBoxError true /PDFXTrimBoxToMediaBoxOffset [ 0.00000 0.00000 0.00000 0.00000 ] /PDFXSetBleedBoxToMediaBox true /PDFXBleedBoxToTrimBoxOffset [ 0.00000 0.00000 0.00000 0.00000 ] /PDFXOutputIntentProfile () /PDFXOutputCondition () /PDFXRegistryName (http://www.color.org) /PDFXTrapped /Unknown

    /Description >>> setdistillerparams> setpagedevice