Saturday, January 26, 2008

Introduction to Borland C++ Builder

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.hYou 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.hThere 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 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"
C++ InstructionsComputer 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: Introduction to FunctionsFrom what we have learned so far, change the contents of your file as follows:#include #include main(){} You can include one function inside of another. As an example, change the main() function as follows:main(){getch();} Executing ProgramsA 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: Executing a ProgramOn the Debug toolbar, click the Run button .As you see, the program does not do much because we did not write a formal assignment To close the DOS window, press Enter on your keyboard. C++' coutAlthough 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 #include main(){ getch();}
The iostream.h library contains a special operator used to display something on the screen. 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 <<> 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 #include 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";

Artikel yang Berkaitan

0 komentar:

Post a Comment