Chapter 2: Delphi and C Language Basics


This chapter deals with the basics of Delphi and C++ programming. It first explains the general structure of a Delphi application by dissecting the source code of the automatically generated console application. After this overview, the chapter shows how to work with variables and how to achieve basic input/output. Finally, the chapter covers the basics of the C++ language.

Structure of a Delphi Application

The best way to learn about the basic structure of every Delphi application is to create a console application project. Create the console application project and take a look at the source code (see Listing 2-1). (If you've skipped the first chapter, select File ® New ® Other on the main menu, select the Delphi Projects node on the New Items dialog box, and double-click the Console Application icon.)

Listing 2-1: Console application

image from book
program Project2; {$APPTYPE CONSOLE} uses   SysUtils; begin   { TODO -oUser -cConsole Main : Insert code here } end.
image from book

Even though the source code generated for the console application is quite short, the basic skeleton of a Delphi application is actually much shorter. To function properly, a Delphi application needs only two lines of code:

begin end.

These two words are known as reserved words.

Reserved Words

Reserved words are an essential part of the programming language, and every reserved word has a special meaning. Reserved words are very easy to spot in Delphi, since they are boldfaced by default. Delphi reserved words are displayed in Table 2-1.

image from book
Table 2-1: Delphi reserved words

and

array

as

asm

begin

case

class

const

constructor

destructor

dispinterface

div

do

downto

else

end

except

exports

file

finalization

finally

for

function

goto

if

implementation

in

inherited

label

library

mod

nil

not

object

of

or

out

packed

procedure

program

property

raise

record

repeat

resourcestring

set

shl

shr

string

then

threadvar

to

try

type

until

uses

var

while

with

xor

image from book

The reserved words begin and end followed by a period represent the main block of the application. All statements that we want to execute when the application starts have to be written inside the main block.

Besides the reserved words that define the main block, the generated console application uses two more reserved words: program and uses.

The reserved word program is used to define the application's name. To properly define an application's name, the word program has to be followed by the application name and a semicolon. The application name defined by the word program should match the project file name (program Asterix = Asterix.dpr).

The reserved word uses represents a list of units that the application needs to successfully compile. Units contain various code segments that we can use in our applications. A more detailed discussion on units can be found in Chapter 5 in the "Creating Units" section.

Comments

Comments are simply segments of source code in which the programmer writes plain text that describes a piece of code. Comments are completely ignored by the compiler, meaning that they aren't compiled into and don't increase the size of the executable file.

There are two types of comments in Delphi — block comments and single-line comments. A single-line comment is written using two slashes:

// begin

A single-line comment is the text between the two slashes and the end of the line. In this example, the Delphi compiler no longer sees the word begin as a reserved word but as a comment.

The single-line comment can be written anywhere in a line, but only the text to the right of the slashes is interpreted as a comment.

The block comment can be written in two ways. One way of writing block comments can be seen in the main block of the generated console application as shown in Listing 2-1. The text between the left and right braces is a block comment. A block comment can even stretch over multiple source code lines:

begin   { TODO -oUser     -cConsole Main :     Insert code here } end.

Another way of writing a block comment is to use the asterisk-parenthesis combination. Use the left parenthesis followed by an asterisk to open the block comment and an asterisk followed by a right parenthesis to close the block comment:

(* uses   SysUtils; *)

You can even nest block comments, but you can only nest comments of different types:

// The following comment is OK. (* begin   { TODO -oUser -cConsole Main : Insert code here } end. *) // This syntax is illegal - "end." is not commented. { begin   { TODO -oUser -cConsole Main : Insert code here } end. } 

In addition to using comments as a means of source code documentation, Borland developers opted to use comments for several other tasks. One of these tasks is to maintain a to-do list for the project. You can see this specialized comment in the main block of the console application.

All to-do comments can be viewed in the To-Do List dialog box. To display the To-Do List dialog box, select View ® To-Do List (see Figure 2-1).

image from book
Figure 2-1: The To-Do list

You can add your own to-do comments by typing them directly into the source code, using the To-Do List dialog box, or right-clicking the Code Editor and selecting Add To-Do Item (see Figure 2-2).

image from book
Figure 2-2: The Add To-Do Item dialog box

Another specialized comment type has the ability to change the behavior of the Delphi compiler. This type of comment is known as a compiler directive.

Compiler Directives

Compiler directives are special comments that can be used to change the default compiler behavior. Compiler directives are written as block comments but with the addition of the dollar sign as the first character inside the braces:

{$APPTYPE CONSOLE}

The $APPTYPE compiler directive is used to tell the compiler to generate a console application or a GUI (windows-based) application. If the $APPTYPE directive is omitted, Delphi compiles the application as a GUI application.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net