Tutorials - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Tutorials

Description:

Don't leave everything to the last minute Come to the Labs. ... Trunc - Truncates a Real to an integer. Random - Returns a random number. 5. High Level Languages ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 46
Provided by: scie205
Category:

less

Transcript and Presenter's Notes

Title: Tutorials


1
Tutorials
  • Do not just rely on the 6 lectures practice is
    vital.
  • Labs every Monday 16.00-18.00 (A31).
  • Tuesday 9.00-11.00 (A31).
  • Dont leave everything to the last minute Come to
    the Labs.
  • Pick up Pascal as soon as possible you will
    need to do some form of self-learning. On the HLL
    website there are links to
  • - Borland on-line tutorials
  • - the ICP module Web site
  • - Object Pascal Language Guide.

2
Last Lecture
  • Last Lecture we covered the following
  • Why Delphi is Goodness
  • Delphi IDE
  • Object-oriented Pascal
  • Pascal Units
  • Variables
  • Operators
  • Strings
  • Decisions Loops

3
This Lecture
  • In Todays Lecture we will cover
  • String Manipulation
  • Mathematical Functions
  • Records
  • Functions and Procedures
  • Pointers
  • Dynamic Memory Allocation
  • Classes
  • That will be the end of Object Pascal.

4
1. Mathematical Functions
  • Ln - Natural Logartihm
  • Sqr - Square
  • Sqrt - Square Root
  • Pi - Pi
  • Cos - Cosine
  • Sin - Some
  • Abs - Absolute Value
  • Frac - Fractional Part of a Real
  • Int - Integer part of a Real
  • Round - Rounds a Real to an integer
  • Trunc - Truncates a Real to an integer
  • Random - Returns a random number

5
2. String and Character Functions
  • In order that you can successfully manipulate
    strings for use in Delphi, you must be familiar
    with the various string and character function
    available in Object Pascal. In the following
    slides, I will cover some of these functions.
  • Always remember that in Pascal a string is a
    character array starting from the 1 not 0
    element.
  • Eg. If Str Jim then str1J, str2i,
    str3m

6
String and Character Functions
  • Object Pascal provides string functions for
  • - Concatenation
  • - Case Conversion
  • - Copying a Substring
  • - Deleting a Substring
  • - Inserting characters into a String
  • - Searching for Characters in a string
  • - Composite Operations (eg. Replace)

7
Chr
  • Chr returns a character with a specified
    ordinal value.
  • Chr(X Byte) Char.
  • var i Integer
  • begin
  • for i 32 to 126 do
  • write(Chr(i))
  • end.

8
Concat
  • Concat concatenates a character with a
    specified ordinal number.
  • Concat(s1,s2,,sn String) String
  • var s string
  • begin
  • sconcat(ABC,DEF)
  • end.
  • ABCDEF

9
Copy
  • Copy returns a sub-string of a string.
  • Copy(S String Index Integer Count Integer)
    String
  • var s string
  • begin
  • s ABCDEF
  • s copy(s, 2, 3)
  • end.
  • BCD

10
Delete
  • Delete deletes a sub-string from a string.
  • Delete(var SString Index Integer Count
    Integer)
  • var s string
  • begin
  • s Big Tim Brailsford
  • delete(s, 9, 10)
  • label1.caption s
  • end.
  • Big Tim

11
Insert
  • Insert inserts a sub-string into a string.
  • Insert(Source String var S String
    IndexInteger)
  • var s string
  • begin
  • s Little Hopkins
  • insert(Kevin , s, 7)
  • label1.caption s
  • end.
  • Little Kevin Hopkins

12
Length
  • Length returns the dynamic length of a string.
  • Length(S String) Integer
  • var s string
  • begin
  • s Aston Villa
  • lenlabel.caption length(s)
  • end.

13
Pos
  • Pos searches for a sub-string in a string.
  • Pos(Substr String SString) Byte
  • var s string
  • begin
  • s 123.5
  • convert spaces to zeros
  • while pos( , s) gt 0 do
  • spos( , s) 0
  • end.

14
Str IntToStr
  • Str converts a numeric value to a string.
  • Str(X Width Decimals var S String)
  • var s string
  • i integer
  • begin
  • I 123
  • str(i, s)
  • end.
  • s contains 123

15
UpperCase LowerCase
  • UpperCase converts a string to upper case
    letters.
  • LowerCase converts a string to lower case
    letters.
  • begin
  • label1.caption UpperCase(Hello)
  • end

16
StrPas StrPCopy
  • The Delphi component classes use null-terminated
    strings. This can be confusing.
  • To convert from a Delphi type string (PChar or
    array of Char) to a Pascal type string, use the
    StrPas function.
  • To convert the other way, use the StrPCopy
    function.

17
Format
  • Format formats and returns a string based on
    the format string and arguments passed, just like
    printf.
  • E.g. the following adds two numbers, then uses
    Format to build a string to report the result
  • var
  • S String
  • X Integer
  • begin
  • X 10 20
  • S Format(The result is d, X)
  • Label1.Caption S
  • end

18
Format
  • You could also do the calculation within the
    Format function.
  • output 20 5 25
  • var
  • X Integer
  • Y Integer
  • begin
  • X 20
  • Y 5
  • Label1.Caption
  • Format (d d d, X, Y, X Y)
  • end

19
3. Records
  • A record is a collection of related data
    identified as a single storage unit, equivalent
    to a C struct.
  • A record is declared as follows
  • Type Person record
  • FirstName string
  • LastName string
  • Address string
  • Age Integer
  • end

20
Records
  • Each variable that you place in a record
    definition is called a field.
  • Once a record has been defined, instances of that
    record can be created for use in your program.
  • To create an instance of a record, define it as a
    new variable
  • var
  • MyRecord TPerson

21
Records
  • You can now assign values to the fields
  • MyRecord.FirstName James
  • MyRecord.LastName Goulding
  • MyRecord.Address Room C14
  • MyRecord.Age 26
  • The dot operator, or structure member selector,
    is placed after the record name and before the
    field name for that record.

22
4. Functions Procedures
  • Functions and Procedures are key to procedural
    programming.
  • Procedural programming improves efficiency and
    clarity by compartmentalising repetitive tasks.
  • You can survive Delphi with just a knowledge of
    procedural programming. Delphi is OO, but the
    component classes are hidden from the user.

23
4. Functions
  • The anatomy of a function in Pascal

24
Procedures
  • A procedure is a section of code, similar to a
    function, except that it does not have a return
    value.
  • The syntax is similar to a function except you
    use the procedure keyword and there is no return
    value type specified.
  • procedure ChangeCaption(x string)
  • var y string
  • begin
  • y The new caption is
  • label1.caption x y
  • end

25
Parameters
  • You can pass variables into a function or
    procedure by value or by reference.
  • When you pass a variable by value into a
    function, the variable passed in is only changed
    locally within the function.
  • Passing by reference makes global changes to a
    variable passed in. To pass by reference, you
    should include a var keyword in front of any
    variables you wish to pass this way.

26
Passing by Value
  • procedure cube(x integer)
  • begin
  • x xxx
  • Label1.caption The answer is x
  • end
  • MAIN PROGRAM
  • var number integer
  • begin
  • number 3
  • cube(number)
  • Label1.caption Label1.caption and
    number is number
  • end
  • Label 1 OUTPUT The answer is 27 and number
    is 3

27
Passing by Reference
  • procedure cube(var x integer)
  • begin
  • x xxx
  • Label1.caption The answer is x
  • end
  • MAIN PROGRAM
  • var number integer
  • begin
  • number 3
  • cube(number)
  • Label1.caption Label1.caption and
    number is number
  • end
  • Label 1 OUTPUT The answer is 27 and number
    is 27

28
5. Pointers
  • A pointer is a data type that contains a memory
    address of a variable.
  • A pointer can point to a variable of any type
    (e.g. an integer, a string or a record).
  • Pointers point to the address of the start of a
    data construct, so they are always the same size,
    regardless of what they are pointing to.

29
Pointer Syntax
  • The Character byte is a pointer that is
    pointing to a byte
  • var pt1 byte
  • pt1 is the data pointed to by pt1
  • writeln(pt1)
  • The _at_ Character _at_myVar is the memory address
    of myVar
  • var myVar byte
  • pt1 byte
  • ...
  • pt1 _at_myVar
  • writeln(pt1)

30
6. Dynamic Memory Allocation
  • Memory Allocation normally automatically
    controlled. But you have to tell the computer how
    big a data sructure is. eg. Arrays
  • The Heap is an area of memory that can be
    allocated while a program is running. (ie.
    Dynamically)
  • Data allocated on the Heap occurs at a random
    address, and you cannot give the data a name.
  • Hence we construct a pointer to tell us where
    our data is.

31
New and Dispose
  • The new procedure allocates memory on the heap
    (dynamically allocatable memory).
  • The dispose procedure de-allocates memory.
  • var myString string
  • begin
  • new(myString)
  • myString This is my string
  • doSomething()
  • dispose(myString)
  • end

32
7. Classes
  • A class is a collection of fields and methods
    (functions) that work together to accomplish a
    specific programming task.
  • The function of a class is to encapsulate a task
    or concept. It contains all of the functions and
    variables relevant to that concept.
  • Hence Classes have Properties (variables) and
    Capabilities (methods).
  • You cannot write a complex program without
    classes. Do not be scared of them. They seem odd
    at first but quickly become second nature and
    speed up your programming.

33
Advantages of Classes
  • Without classes Windows would not exist.
  • Classes are Reusable.
  • Libraries of reusable Classes (Class Libraries)
    make sophisticated functionality easily
    available.
  • This is a construction kit approach to
    programming
  • For example, consider graphical programs
  • - Window Class
  • - Text Box Class
  • - Button Class
  • - Label Class

34
A Class Example
  • Consider a check-box Windows control component as
    an example.
  • The class which represents a check-box would have
    fields for the caption of the check-box and for
    the state (checked or unchecked).
  • There may be methods such as GetCheck, SetCheck,
    GetCaption and SetCaption.
  • You can make separate instances (objects) of the
    class to control individual check-boxes.

35
Anatomy of a Class
  • Classes have the following features
  • Declaration
  • Access Control
  • Constructor
  • Destructor
  • Variables
  • Methods
  • A Special Pointer called self

36
Access Control
  • A class, like a record, has a declaration. The
    class declaration is always in a type section.
  • Classes have four levels of access
  • Private
  • Public
  • Protected
  • Published

37
Anatomy of a Class
  • The public part of a class allows access to the
    outside world.
  • The private part of a class is the internal
    implementation which is hidden from the outside
    world (data abstraction).
  • The protected access level is similar to private,
    except its contents can be accessed by classes
    that are derived from the class.
  • The published section publishes components.

38
Example Class Code
  • Type
  • Tvehicle class
  • private
  • Started Boolean
  • Speed Integer
  • procedure StartEngine()
  • protected
  • procedure StartUpProcedure()
  • public
  • HaveKey Boolean
  • Start Boolean
  • procedure Shutdown()
  • end

39
The Constructor
  • The constructor is a method that is used to
    create an instance of a class.
  • If you dont provide a constructor, the base
    class constructor is called when you create the
    class (called Create).
  • It must be declared using constructor keyword
  • constructor Create constructor placed in
  • public section

40
Instantiation of an Object
  • An object is an instance of a class.
  • Instantiation is the creation of an object.
  • var MyCar Tvehicle
  • begin
  • MyCar Tvehicle.Create
  • end

41
Destructors
  • A destructor is a method which is automatically
    called before an object is destroyed.
  • Type Example class
  • private
  • ...
  • public
  • constructor Create
  • destructor Destroy
  • end
  • destructor Tvehicle.Destroy
  • begin
  • ...
  • end

42
Methods
  • Methods are functions or procedures that belong
    to a class.
  • They are local to the class and can only be
    called from within a class itself or an instance
    of a class.
  • Methods can be declared in the private,
    protected, or public sections of a class.

43
Delphi Classes and Objects
  • The next few slides will cover the basics of
    classes and objects already in Delphi
  • A set is a collection of values of one type.
  • Consider the style property of a font object.
    This property can include one or more of the
    following values
  • fsBold
  • fsItalic
  • fsUnderline
  • fsStrikeOut

44
Using a Set
  • Lets say you have an instance of the FontStyle
    object and you want to add bold and italic styles
    to the set of properties for that instance.
  • This is how you would do it
  • var
  • Styles TFontStyles
  • begin
  • Styles Styles fsBold, fsItalic
  • Memo.Font.Style Styles
  • end

45
Labs
  • First Labs occur on Monday after the FAI lecture.
  • There will be a new example sheet in each Lab.
  • We will generally only cover simple things Hello
    World program, basic event handling, label
    displaying and file input/output.
  • It is up to you to put in time with the on-line
    tutorials and try to learn some features for
    yourself )
Write a Comment
User Comments (0)
About PowerShow.com