Appendix L - Borland C++ Builder

14
1 Appendix L Introduction to Borland C++ Builder 5.0 This appendix serves as a quick reference for performing the following operations 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 Opening an existing project Creating multi-file projects Starting a New Project The first step in creating a program with Borland C++ Builder is to start a project. A project is a group of one or more files that make up a software application. (Even if your program consists of no more than a single source code file, it still must belong to a project.) To start a project: 1. Launch Borland C++ Builder. The IDE windows open on the desktop, similar to what is shown in Figure L-1. AppendL.fm Page 1 Monday, August 2, 2004 3:30 PM

Transcript of Appendix L - Borland C++ Builder

Page 1: Appendix L - Borland C++ Builder

1

Appendix L

Introduction to

Borland C++ Builder 5.0

This appendix serves as a quick reference for performing the following operations using theBorland C++ Builder integrated development environment (IDE):

Starting a new project and entering code

Saving a project to disk

Compiling and executing a project

Opening an existing project

Creating multi-file projects

Starting a New Project

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

project

. A project is agroup of one or more files that make up a software application. (Even if your program consists ofno more than a single source code file, it still must belong to a project.)

To start a project:

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

AppendL.fm Page 1 Monday, August 2, 2004 3:30 PM

Page 2: Appendix L - Borland C++ Builder

2

Introduction to Borland C++ Builder 5.0

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

. You see the dialog box shown in Figure L-2. (Make sure the

New

tab is selected.)

Figure L-1

Figure L-2

AppendL.fm Page 2 Monday, August 2, 2004 3:30 PM

Page 3: Appendix L - Borland C++ Builder

Introduction to Borland C++ Builder 5.0

3

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-3 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-4.

Figure L-3

Figure L-4

AppendL.fm Page 3 Monday, August 2, 2004 3:30 PM

Page 4: Appendix L - Borland C++ Builder

4

Introduction to Borland C++ Builder 5.0

5. The right-most pane in the window shown above is the text editor. C++ Builder automati-cally creates a program “skeleton” for you. The skeleton contains some preprocessor direc-tives 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-5 shows a simple program that has been written in the editor after the default program skeleton has been erased.

Saving Your Project to Disk

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

File

onthe menu bar, and then click

Save Project As…

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

OK

button if you wantto keep this name. If you wish to save the source file under a different name, enter the new name,and click the

OK

button. Next, a Save As dialog box appears for the project file. A default name,such as Project2.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 new name,and click the

OK

button.After you have saved the project the first time, you may save it subsequent times by clicking

File

on the menu bar, then clicking

Save All

.

Figure L-5

AppendL.fm Page 4 Monday, August 2, 2004 3:30 PM

Page 5: Appendix L - Borland C++ Builder

Introduction to Borland C++ Builder 5.0

5

Opening an Existing Project

To open an existing project, click

File

on the menu bar, then click on

Open Project…

Use theresulting dialog box to browse to the location of your project file. When you have located yourproject file, double-click it.

Compiling and Executing

Once you have entered a program’s source code, you may compile and execute it by any of the fol-lowing 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 code where the errorwas encountered.

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

cout

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

Figure L-6

AppendL.fm Page 5 Monday, August 2, 2004 3:30 PM

Page 6: Appendix L - Borland C++ Builder

6

Introduction to Borland C++ Builder 5.0

If the program compiles successfully, an MS-DOS window appears and the program runs. This isillustrated in Figure L-7.

N

OTE

:

T

he program shown in the example above has a

cin.get()

statement as the last statement in the program. The statement causes the program to pause until the user presses the Enter key. The statement is in the program because the MS-DOS window automatically closes as soon as the program is finished. If the program above did not have the

cin

.

get

(

)

statement, it would quickly flash the message "This is a simple program" on the screen, and the MS-DOS window would suddenly close.

Sometimes a single

cin.get()

statement will not pause the screen. (This is because a prevous

cin

statement has left a newline character in the keyboard buffer.) A better way of pausing thescreen is to include the

conio.h

header file, and use the

getch()

function at the end of the pro-gram. This is demonstrated in the next example.

Creating a Multi-File Project

Many of your programs consist of more than one file. For example, consider the followingprogram:

// This program demostrates a simple class#include <conio.h> // For the getch() function#include <iostream>#include "rectang.h" // Contains Rectangle class declarationusing namespace std;

// Don't forget to link this program with rectang.cpp!

Figure L-7

AppendL.fm Page 6 Monday, August 2, 2004 3:30 PM

Page 7: Appendix L - Borland C++ Builder

Introduction to Borland C++ Builder 5.0

7

int main(){

Rectangle box;float wide, long;

cout << "This program will calculate the area of a\n";cout << "rectangle. What is the width? ";cin >> wide;cout << "What is the length? ";cin >> long;box.setData(wide, long);box.calcArea();cout << "Here rectangle's data:\n";cout << "Width: " << box.getWidth() << endl;cout << "Length: " << box.getLength() << endl;cout << "Area: " << box.getArea() << endl;getch();return 0;

}

This program relies on two other files:

Rectang.h

and

Rectang.cpp

. Before compiling this pro-gram,

rectang.h

and

rectang.cpp

must be added to the project. The steps listed below showyou how to set the project up with all three files as members.

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

Starting a New Project

, at the beginning of this appendix.

2. Enter the main C++ source code (shown above) into the text editor. Your editor window will look similar to Figure L-8.

Figure L-8

AppendL.fm Page 7 Monday, August 2, 2004 3:30 PM

Page 8: Appendix L - Borland C++ Builder

8

Introduction to Borland C++ Builder 5.0

3. Now insert the

Rectang.cpp

source file into the project. Click

Project

on the menu bar. On the Project menu, then click

Add To Project…

The

Add to Project

dialog box. Use the dialog box to find the file

Rectang.cpp

in the Appendix L folder on the student disk. When you locate the file, click it and then click the

OK

button. The file

Rectang.cpp

is now a member of the project. Your editor window now looks similar to Figure L-9.

4. At this point, the main source file and

Rectang

.

cpp

are members of the project. At the top of the editor window are tabs for the two files. Click on either tab to bring the file’s contents into view.

The left pane of the editor window, which is called the

Class Explorer

, is used to bring class decla-rations into the editor window. In the figure shown above, a folder titled Project2 – Classesappears. (Your screen will look similar, but the project name may be different.) Click the small

+

sign to expand the view. The window looks similar to Figure L-10.

5. Click the small + sign next to Rectangle. The view expands further to show entries for all the members of the Rectangle class. Your window should be similar to Figure L-11.

Figure L-9

Figure L-10

AppendL.fm Page 8 Monday, August 2, 2004 3:30 PM

Page 9: Appendix L - Borland C++ Builder

Introduction to Borland C++ Builder 5.0

9

6. By double-clicking any of the Rectangle class’s member names in the Class Explorer window, you bring the source file containing the member’s declaration into the editor. For example, by double-clicking the

area

member, the

Rectang.h

file is brought into the editor, as shown in Figure L-12.

7. You may compile and execute the project by following the steps listed under the section

Com-piling and Executing

.

Figure L-11

Figure L-12

AppendL.fm Page 9 Monday, August 2, 2004 3:30 PM

Page 10: Appendix L - Borland C++ Builder

10

Introduction to Borland C++ Builder 5.0

Removing a File from a Project

To remove a file from a project, click Project on the menu bar, then click Remove from Project…The Remove from Project dialog box appears, as shown in Figure L-13.

Select the name of the file you wish to remove from the project and click the

OK

button. The filewill be removed from the project. (It will not be erased from the disk, however.)

Opening and Running an Existing Program

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

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

New

tab is selected.)

Figure L-13

Figure L-14

AppendL.fm Page 10 Monday, August 2, 2004 3:30 PM

Page 11: Appendix L - Borland C++ Builder

11

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. Then, click the

Specify project source

checkbox. At this point, you may either type the name and location of the file in the textbox, or click the button to browse for the file. Figure L-17 shows an example of the dialog box where the file

C:\Programs\Chap2\pr2-1.cpp has been selected.

5. Click the OK button. Your screen now looks similar to Figure L-18. The program you selected is loaded into the text editor.

Figure L-15

Figure L-16

AppendL.fm Page 11 Monday, August 2, 2004 3:30 PM

Page 12: Appendix L - Borland C++ Builder

12 � Introduction to Borland C++ Builder 5.0

6. You may compile and execute the program by any of the 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 code where the errorwas encountered.

If the program compiles successfully, an MS-DOS window appears and the program runs.

Figure L-17

Figure L-18

AppendL.fm Page 12 Monday, August 2, 2004 3:30 PM

Page 13: Appendix L - Borland C++ Builder

Introduction to Borland C++ Builder 5.0 � 13

Where Files are CreatedWhen you save a new project, you normally save all of the files associated with that project in thesame folder. (See the Saving Your Project to Disk section.) This folder is known as the project folder.

For example, suppose we’ve created a project named Lab5, and saved it in the folderC:\MyProjects\Lab5. All of the C++ source files that you have created for this project, as well asthe project file that ends with the extension .bpr, will be stored 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 project folder is thedefault location for all data files. For example, suppose our Lab5 project creates a file with the fol-lowing 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 existing filefor reading, it will look in the project folder by default. For example, suppose our Lab5 programattempts 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 project folder,C:\MyProjects\Lab5.

If we want to open a file in a location other than the project folder, we must provide a path aswell as a file name. For example, the following statement opens a file named data.txt in theC:\Data folder.

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

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 beginning of an escapesequence. In order to represent a backslash as a character in a literal string, you must use twobackslashes.

Here’s another example:

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

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

AppendL.fm Page 13 Monday, August 2, 2004 3:30 PM

Page 14: Appendix L - Borland C++ Builder

14 � Introduction to Borland C++ Builder 5.0

AppendL.fm Page 14 Monday, August 2, 2004 3:30 PM