concepts - Study Opportunities

advertisement
IT is gr8!
A workshop for IT teachers
2015
Preparing for programming concepts
• Data types
• Arrays (lists)
• OOP
• Classes and objects
• Attributes and behaviour of objects
• Data transfer to and from an object
Programming concepts: Data types
All the operators work with numbers.
All the slots are round.
There is a difference between integer
and real values.
Round is a function.
It receives one argument.
It returns an integer.
Programming concepts: Data types
All the operators work with text (strings).
All the slots which can accept text are
rectangular.
The slot which contains a number (3) is
round.
34251 is treated as a string because it was
entered as the argument of the length
function.
Programming concepts: Data types
When you compare values you create a condition.
The result of a condition is a Boolean value.
Boolean values fit into slots with pointed
sides.
Programming concepts: Arrays
Programming concepts: OOP
1. Re-use of code (code stored in a unit)
2. Passing data to pre-written code
3. An object has attributes (data) and
behaviour (methods)
4. Data encapsulation
a) Data can only be passed to, and obtained
from an object using methods.
b) An object’s methods act on its own data.
Why OOP?
Delphi is an OOP language.
It has been developed using objects.
You have already used objects.
You are going to learn how to create your
own objects.
Where have you used objects?
• Buttons, Panels, Edits (all are
components – a special type of object).
• These components have been created
according to a ‘blueprint’ or ‘recipe’
called a class.
You have used static components, and instantiated components
dynamically
‘Static’ components
Dynamic components (instantiated while program is running)
• MyPanel := TPanel.Create(frmPicture);
• MyLabel := TLabel.Create(MyPanel);
• MyImage := TImage.Create(MyPanel);
Classes
The definitions (blueprint / recipe) for
components are called classes and they
are stored in units.
How to instantiate and use an object based on a class
1. Use the unit where the class has been
declared.
2. Declare a variable based on the class.
3. Instantiate the object.
4. Call methods from the object.
5. Free the object.
1.
2.
3.
4.
5.
Grade 11
Chapter 13
Activity 3.1
Delphi also provided other classes which can be used to
instantiate objects. We want to create our own classes
and instantiate objects based on these classes. So we
need to know how an object is structured . . .
What does an object look like?
• All objects have attributes (describing
what the object looks like) and
• behaviour (indicating what the object
can do).
For example . . .
The various attributes and
the behaviour have been
indicated in a class definition
(a recipe or blueprint) which
is stored in a unit. For
example the TEdit class
definition is stored in the
StdCtrls unit.
Components are visible objects. You can
also use objects which are not visible at
run-time of the program, for example a
TStringList.
A StringList has
Attributes
 Text (an array/list of strings)
 Count (an integer indicating the number
of strings in the list)
and
Behaviour







AddStrings
Delete
Exchange
Sort
IndexOf
SaveToFile
LoadFromFile
The TStringList class definition
is stored in the Classes unit.
How to instantiate and use an object based on a TStringList class
1.
2.
3.
4.
5.
Grade 11
Chapter 13
Activity 5
Benefits of using the TStringList class
• Learners can see the benefit of using
classes.
• They experience the concept of data
encapsulation.
• They want to create their own classes
or collaborate and exchange classes.
Passing data to a subroutine
Re-confirming concepts
Function Power(Base: Extended; Exponent: Extended): Extended;
rAns := POWER(StrToInt(edtBase.Text), StrToInt(edtPower.Text));
Passing data to an object
Re-confirming concepts
Write your own class and instantiate an object
When you write many programs, and more
complicated ones, it may be necessary to create
your own classes, so you can create the same
objects in different programs, or so you can do
more complicated programming in a separate
entity.
We are going to use an uncomplicated object to
begin with: A waiter as an object.
A Waiter has
Attributes
 Name
 Number of hours the waiter
worked
 Rate the waiter earns per hour
and
Behaviour




Provide his/her name
Provide the number of hours worked
Provide the rate per hour
Calculate and provide total wage
The TWaiter class definition
will be stored in the
clsWaiter_u unit. (You
choose the name of the unit
yourself.)
How to instantiate and use an object based on a TWaiter class
1.
2.
3.
4.
5.
Grade 12
Chapter 1
Activity 1.2
An object contains attributes and behaviour
Re-confirming concepts
• A Form is an object (component).
• While you are building a new Form,
you are adding attributes (components
and variables with class scope).
• You are adding behaviour (event
handlers and methods – functions and
procedures).
Attributes
Behaviour
Prepare for concepts
frmMain
frmHideCache
frmSwop
frmSearch
Use OOP principles
Methods are
part of the class
of the Form.
Methods work with attributes
(data) of the Form.
Do NOT use procedural principles
Subroutines - not
methods.
Subroutines work with a
variable which has unit scope.
Download