0672320606
- Chapter 3: Basic Pascal Syntax
- Integer Types and Floating-Point Types
- Pascal Strings
- Typecasts
- Arrays
- Records
- Pointers
- What You Won't Find in Object Pascal
- Summary
Chapter 3: Basic Pascal Syntax
By Charlie Calvert
This chapter is dedicated to some of the more advanced features of the most commonly used Pascal types. Subjects covered include integers, strings, floating-point types, arrays, records, and pointers.
I'm writing for experienced programmers, and the text is not meant to be a primer on Object Pascal for newcomers. I assume that the reader already understands simple types such as integers, floating-point types, and even complex types such as arrays, records, and pointers. I will highlight the unexpected or advanced features of these types as they are implemented in Object Pascal.
Some readers might have last used Pascal many years ago and might no longer have a sure feeling of the language. Many programmers have had experience with ANSI Pascal but not with Object Pascal. Object Pascal is to ANSI Pascal as C++ is to C. If you're expecting the Object Pascal universe to resemble the relatively bland world of ANSI Pascal, then you may be in for some surprises.
The material in this chapter will be necessary for an understanding of the material in Chapter 5, "The Editor and Debugger." In short, I can't discuss the finer points of writing and debugging code in the Object Pascal editor without first covering a few crucial syntactical issues.
DELPHI NOTE
Experienced Object Pascal programmers will want to at least skim this chapter because there might be some issues such as variant records or dynamic arrays that you want to bone up on. Advanced topics are spread throughout the chapter. For instance, one section that even experienced Delphi users should read is "The TBcd type and Floating-Point Accuracy," which covers the new routines for handling the TBcd type.
The Content of This Chapter
Pascal is a strongly typed language. The compiler cares a great deal about your type declarations, and the things you can do with variables are severely restricted by your type choices. In the old days, this was considered a bad thing because it was felt to be restricting. Over the years, however, the general consensus on this topic has changed. All modern languages, such as C++ and Java, have followed in Pascal's footsteps by placing a strong emphasis on typing.
Pascal was invented back in 1968 and was first implemented in 1970. Any language that old is going to have some quirks in it. However, overall, I consider Pascal to be a very good language. If you come to this book with a different prejudice, I ask that you put those ideas aside while you read this chapter. You might be surprised at the power of this language. In particular, C++ programmers probably will be surprised to find that the language is so flexible, and Java programmers will be surprised to find that it has so many modern features.
Most major Pascal types will be covered in one fashion or another. However, there are two major topics that I will just touch on:
ObjectsThe Object Pascal syntax for declaring and building objects will be explored in Chapter 4, "Objects and Interfaces," and also throughout nearly all the chapters in Part II, "CLX." However, this chapter covers allocating and deallocating memory for objects. You will find this material in this chapter's coverage of pointers. In particular, you should see the sections "Working with Pointers to Objects" and "Pointers, Constructors, and Destructors." Included are a brief overview of the Pascal constructor and destructor syntax and a few words on writing virtual methods.
InterfacesInterfaces enable you to define the structure for a class without creating an implementation for it. I will cover this important topic in Part II.