Home

Introduction to Borland C++ Builder

 

Fundamentals

 

Introduction

There are various ways you can launch the program. The most common consists of clicking.

To create a shortcut on the desktop, in Microsoft Windows higher than Win95, click Start -> Programs -> Borland C++ Builder 6, right-click C++ Builder 6 and click Send To -> Desktop (Create Shortcut)

  • To start Borland C++ Builder, click Start, position the mouse on Programs, position the mouse on Borland C++ Builder, C++ Builder 5
    Borland C++ Builder IDE

 

This site uses the -> arrow for the menu requests.
From now on, on this site,
Request Means
Edit -> Copy Click Edit then click Copy
View -> Toolbars -> Custom Click View position the mouse on Toolbars, and then click Custom
 
From now on, on this site,
Press Means
T Press the T key
Alt, G Press and release Alt. Then press G
Ctrl + H Press and hold Ctrl. While you are still holding Ctrl, press H once. Then release Ctrl
Ctrl + Shift + E Press and hold Ctrl. Then press and hold Shift. Then press E once. Release Ctrl and Shift
 

The Code Editor

One of your most regular jobs will consist of writing code that directs the computer as to what to do, when, and how. This is done in an appropriate window called the Code Editor.

The Code Editor is a full-featured text editor adapted for coding purposes. It is programmed to identify the parts of a program that are recognized by C++ or not. Therefore, the Code Editor uses different colors to differentiate the various categories of words used in a program. You can customize its display of color for each type. To do that, on the main menu, you would click Tools -> Editor Options. In the Editor Properties dialog box, click the Colors Tab.

By default, the Code Editor displays its text with a white background. Other general background colors are available from the Color SpeedSetting combo box. For example, if you select Classic, you would have a navy background.

The items that display in the Code Editor are organized by categories, and each category can have its own color. The Editor Properties provides 16 fixed colors. To change the color for a category, click it in the Element list box. To change its color, choose one from the Color panel. To change its style, use the check boxes in the Text Attributes section. The color you select for text of the category is referred to as the foreground color. You can specify a color that would permanently highlight words of that category. This is done with the Background check box:

The Editor Properties Dialog Box

While configuring the colors, you can get a preview in the memo box of the lower or lower-right section of the dialog box.

The Code Editor manages your jobs by organizing files into property sheets (also called tabs). If your project contains more than one file, you can click the desired tab to access one of the files. The basic building block of a program is called a C++ file, and Borland calls it a Unit. Whenever you start Bcb, C++ Builder creates a starting project that has a C++ file called Unit1 while the project is called Project1. Eventually, you will change these names to those you like. A typical code of a form, such as the one we have now, is built from at least two files: a header file and a source file. The file displaying now is called the source file; it gives direct instructions to the computer as to what to do on the form and why. The foundation of this source file (which is also the foundation of the form) is in a file called the header file. By default, Bcb does not display this file at startup; you have to request it.

To display the header file, you can right-click the source file and click Open Source/Header File. Indeed, this action is used to toggle both displays. Since the source and the header files go in pairs (when using classes), they hold the same name but different extensions.

The Class Explorer

What is called an object in real world is also referred to as an object in C++, and an object is built using a class. To organize the objects involved in a program, C++ Builder uses a special window called the Class Explorer. As its name implies, it is used to navigate to various objects.

Author Note Whenever the Class Explorer is not displaying, to get it, on the main menu, click View -> Class Explorer

The Class Explorer is positioned on the left side (or section) of the Code Editor. It is organized in a tree view format with the name of the project as the root. To view the objects that part of a project, expand the tree.

You can close or hide the Class Explorer any time and bring it back at will.

Introduction to C++

 

Introduction

Borland C++ Builder is based on the C++ language but provides a highly developed environment for building applications. Although it looks impressive and easy, you need a (good) foundation in C++ before building such applications. The purpose of this book is to lay that foundation by learning the C++ language first.

C++ is such a huge language that part of its foundation is provided to you so that you can write your applications by adding to it. A program is made of various objects that you use to build your applications. Some of these objects have already been created and are supplied to you when using an environment such as Bcb. Although you will not see the whole functionality of these objects, you should be aware of which ones exist and how to use them. These objects are provided in files called Header Files or libraries. By default, Borland C++ Builder's libraries are located in the C:\Program Files\Borland\CBuilder folder. Those used for C++ are installed in the C:\Program Files\Borland\CBuilderX\Include. Those you will use when creating visual applications are located in the C:\Program Files\Borland\CBuilderX\Include\Vcl folder. Although you are allowed to view these files, if you open any of them, make sure you do not touch anything; even if you see a comma that does not make sense, do not correct it.

A file that you will use as a foundation for your application is called a Header File, this is because, as "head", this file controls some aspects of your application. Therefore, you will place it at the "head" section of your program. When placing a particular file at the head of your program, you are said to "include" it. As headers, these files have an extension of .h

The most used file in C++ is called iostream.h. This file is used to display things on the screen monitor or to get things the user types using a keyboard. To use such a file, you have to "include" it using the include keyword. Here is an example:

include file.h

You must include the extension; this allows C++ to know that you are including a header file. As a rule, when including a file, you must precede the line with a # sign. Therefore, our include line would be

#include file.h

There are usually two kinds of header files you will be using in your programs: those supplied to you and those that you create. You will mostly create your own files in the same folder where you create your program. There are two ways you let C++ know where a header file is located. If the file is located in the C:\Program Files\Borland\CBuilderX\Include, which means that it was supplied to you, include it as:

#include <file.h>

If you created your own file, which means that the file is probably located in the same folder you are creating your application, include it as follows:

#include "myfile.h"
 

Practical Learning Practical Learning: Introducing C++

  1. To view a list of the libraries that will be useful when using C++, open Windows Explorer (or Windows NT Explorer or My Computer).
  2. Navigate to the C:\Program Files\Borland\CBuilderX\Include folder or where Bcb is installed
  3. Scroll down in the list and make sure you see some files such as cmath.h, conio.h, or iostream.h:
     
  4. On the Taskbar, click Bcb to restore it.

Creating a Console Application

Borland C++ Builder allows you to create various types of applications. In this book, we will use only C++ to create console applications. A console application is one that displays its result in a DOS window. The user interacts with it by using the keyboard with very little involvement with the mouse.

Practical Learning Practical Learning: Creating a Console Application

  1. On the main menu of Bcb, click File -> New… If you are using Borland C++ Builder 6, click File -> New -> Other…
     
    The New Items Dialog Box
  2. On the New Items dialog box, click Console Wizard
  3. Click OK. 
  4. In the Console Wizard dialog, click the C++ radio button and make sure that only the Console Application check box is checked (for this application):
     
    The Console Wizard Dialog Box
  5. Click OK.
  6. If you receive a message box asking you to save changes made to a project, click No.
  7. After a few seconds, a skeleton file is ready for you.
    In order to make sure we start from scratch, delete everything in that file and make it empty.
  8. From what we learned earlier, in the empty file, type:

    #include <iostream.h>
  9. To include more than one file, you can write one under the other. For example, to include the conio.h file, change the file as follows:

    #include <iostream.h>
    #include <conio.h>
     

C++ Instructions

Computer programming is the art of telling the computer what to do, when to do it, and how to do it. The programmer accomplishes this by giving instructions to the computer. An example of an instruction is, "Get a number from the user", or “Display a picture on the screen". The computer carries a lot of such assignments. In C++, an assignment is called a function; it is a task you ask the computer to perform to successfully render an intended result. Like any of the objects you will be using in your programs, a function has a name. The name of a function is a letter or a combination of letters and digits. To differentiate the name of a function from other objects, the name of a function is followed by parentheses. As an assignment, a function is made of a set of instructions. These instructions are listed in a group of lines called the body of the function. The body of a function starts with an opening curly bracket "{" and ends with a closing curly bracket "}"; everything in between is part of the function. Therefore, to use a function in your program, its syntax is:

FunctionName() {}

Actually, a function is a little more than that, but for now, this is enough for us.

The most used function in C++ is called main. The main() function can take various forms, the easiest version is as follows:

main() {}

A C++ file is made of statements. These are sequences of actions you ask C++ to perform while the program is running. You can write such a statement on one line, or you can span more than one line. By default, when you start a new line, C++ believes that you are starting a new statement. To let C++ know when a statement ends, you write a semi-colon at the end of the line. For example, to write one statement, you could use:

Here-Is-A-C++-Statement;

To span more than one line, type a semi-colon only when the statement is over. Here is an example:

A-Long-Statement-From-C++-That-Spans-
More-Than-One-Line;

Practical Learning Practical Learning: Introduction to Functions

  1. From what we have learned so far, change the contents of your file as follows:

    #include <iostream.h>
    #include <conio.h>
    main(){}
  2. You can include one function inside of another. As an example, change the main() function as follows:

    main(){getch();}

Executing Programs

A program would not mean much unless it accomplishes the desired purpose. To examine how your development is proceeding, as a beginning programmer, you should regularly ask C++ to show you the result.

The (small) program we have written is really plain English, something we can read and analyze. Unfortunately, the computer does not understand what it means; this is because the computer "speaks" its own language called the Machine Language. For the computer to understand what your program means, you need an intermediary program that can translate your English to machine language and vice versa. This program is called a compiler, and it is supplied to you. The process of executing a program is done in various steps that Borland C++ Builder can resume as one.

There are three ways you can execute a program in Borland C++ Builder. To execute a program, you can press F9, you can also use the main menu where you would click Run ª Run. On the toolbar, you can also click the Run button 

Practical Learning Practical Learning: Executing a Program

  1. On the Debug toolbar, click the Run button Run.
    As you see, the program does not do much because we did not write a formal assignment
  2. To close the DOS window, press Enter on your keyboard.

C++' cout

Although it worked fine, the program we have just used lacks many things. You should make your programs easy to read and navigate. This is accomplished by writing each statement on its own line. For example, the above program can be re-written as follows:

#include <iostream.h>
#include <conio.h>
main()
{
    getch();
}

The iostream.h library contains a special operator used to display something on the screen.

Author Note cout is a class and not an operator. For now, we will call it an operator.

This is done with the cout operator (pronounce "see-out"). To use this operator, type it followed by two "less than" signs "<<", the statement you want to display, and end the line with a semi-colon. An example would be:

cout << Line-Of-Statement;

There are mainly two kinds of items you display with a cout operator: expressions or strings. An expression is a character, a symbol, a word or a combination of those, usually from performing an operation or an assignment. There are specific rules we will learn and follow when displaying an expression. A string is a character, a symbol, a word or a group of words that you ask the compiler to display "as is". To display a string, you enclose it between a pair of double-quotes. For example, to display C++ Programming, you would write:

cout << "C++ Programming";

You can also display a long string and span more than one line. In this case, cout would detect the end of the string when it finds a semi colon after the second double-quotes. There are two methods you can follow. To display a long string, after the cout operator and its <<, start the string with double-quotes; include each line in double-quotes; the compiler will append each line to the next. Here is an example:

cout << "In 1873 Mark Twain and Charles Dudley Warner created an enduring "
"label for their times when they collaborated on a novel entitled "
"The Gilded Age. Ref: America A Narrative History.";

To display sections of strings, you can use one cout but each line should start with its own << operator. Here is an example:

cout << "The White House "
<< "Pennsylvania Avenue";

Indentation is another feature that makes your program easy to read. Indentation is a technique of grouping lines of code by category. To effectively use indentation, identify some special symbols in your program. For example, we saw that the body of a function starts with an opening curly bracket “{“ and ends with a closing curly bracket “}”. To delimit the items that are inside of a function, you should indent them by two empty spaces or one tab. When inside of a function, if you press Tab, C++ Builder refers to the indentation amount specified in the Editor Properties. By default, C++ Builder sets a Tab press to 8 characters, which usually seems too much. Most programmers, including me, want a Tab to be equivalent to 4 characters. To change the indentation amount when pressing Tab, open the Editor Properties dialog box (Tools -> Editor Options…) and click the General property sheet. In the Tab Stops combo box, specify the amount you want and click OK:

Using indentation, the program could be written:

#include <iostream.h>
#include <conio.h>
main()
{
    getch();
}

Indentation should be incremental. That is, when a line of code appears to be a child of the previous line, the new line should be indented. The address lines we saw earlier can be written as:

cout << "The White House "
     << "Pennsylvania Avenue";

Practical Learning Practical Learning: The cout Extraction Operator

  1. Change the content of the file as follows:
     
    #include <iostream.h>
    #include <conio.h>
    
    main()
    {
        getch();
    }
  2. To use the cout operator, change the main() function as follows:
     
    #include <iostream.h>
    #include <conio.h>
    
    main()
    {
    cout << "Press any key to continue...";
        getch();
    }
  3. To execute and test your program, on the main menu, click Run -> Run
  4. To close the DOS window, press Enter, and return to Bcb. 

C++ Comments

Comments in a C++ file serve as documentation entities that help you and other people to read your program. Comments are lines of text that you can write in plain English because the compiler skips them completely.

There are two styles of comments in C++. To write a comment on one line, start the line with two forward slashes. Here is an example of a comment:

// The compiler doesn’t read this line.

To span a comment on more than one line, you can start each line with //. An alternative is to include the comment section between /* and */. Here is an example:

/* The compiler will ignore this whole section and will jump over it.*/

Practical Learning Practical Learning: Using Comments

  1. Change the content of the file as follows:
     
    // Introductory lesson to C++
    #include <iostream.h>
    #include <conio.h>
    
    /* This is the main function.
       It is included on every C++ program.
       It is the central point of a C++ application.*/
    
    main()
    {
        cout << "Press any key to continue...";
        getch();
    }
    // -------------------------------------------
  2. Execute the program to see the result

Escape Sequences

An escape sequence is a character that holds a special meaning to the compiler such as switching to the next line or adding a tab indentation. Although an escape sequence is a character by definition, it is created using the backslash “\” followed by a specific character. For example, the escape sequence \n asks the compiler to add or create a new line, also referred to as Carriage Return or “Carriage Return – Line Feed”. The new line can also be created with a stand-alone endl. Here is an example:

cout << endl;

To use an escape sequence by itself, you can include between single quotes: ‘\n’ or double quotes: “\n”. Because escape sequences are specially recognized by the compiler, they can be included in a cout string. Since the compiler will still read a string, if it finds an escape sequence, it acts accordingly. Here is a list of escape sequences and their meanings:

Escape Meaning
\a Produces a bell sound
\b Backspace
\f Formfeed
\n Newline – Linefeed
\r Carriage return
\t Horizontal Tab
\V Vertical Tab
\\ Backslash
\' Single Quote or Apostrophe
\" Double-Quote
\? Question Mark
 

 

Practical Learning Practical Learning: Using Escape Sequences

  1. To implement examples of using escape sequences, change your program as follows:
     
    // Introductory lesson to C++
    #include <iostream.h>
    #include <conio.h>
    
    /* This is the main function.
    It is included on every C++ program.
    It is the central point of a C++ application.*/
    
    main()
    {
        cout << '\t' << "- Borland C++ Builder -\a" << endl;
        cout << "This will end our session today" << "\n";
    
        cout << "\n\nPress any key to continue...";
        getchar();
    }
  2. To test the program, press F9
  3. After using the program, return to Bcb

Saving a Project

A program in Borland C++ Builder is called a project. As an application, it is saved in a few steps. To save a project, on the Standard toolbar, you can click the Save All button.

Practical Learning Practical Learning: Saving a Project

  1. On the Standard toolbar, click Save All Save All
  2. Type Main to replace the name of the Unit1
  3. Click the arrow of the Save In combo box and click the My Documents
  4. Click the Create New Folder button Create New Folder
  5. Type Exercise01 and press Enter
  6. Double-click Exercise01 to display it in the Save In combo box
  7. Click Save
  8. Type Exercise to replace the name of the project
  9. Click Save

 

 

Home Copyright © 2005-2012, FunctionX, Inc. Next