Introduction to Borland C++ Builder |
|
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)
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:
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.
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.
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.
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: Introducing C++ |
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.
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;
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
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:
The iostream.h library contains a special operator used to display something on the screen.
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:
To display sections of strings, you can use one cout but each line should start with its own << operator. Here is an example:
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:
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:
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.*/
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:
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.
|
|
||
Home | Copyright © 2005-2012, FunctionX, Inc. | Next |
|