Tutorial - WRITING A CLASS LIBRARY
1 * CLASS is TYPE (2/16)
2 * Writing Class in style of OOP (2/17)
3 * Whai is Enumerate? (2/18)
4 * Method Declaration of Class(2/19)
5 * Method of Calculation sample (2/20)
6 * Testing Class you had created. (sample of TVAT
from 2/20) (2/21)
7 * Instance and CLASS (2/22)
8 * Save Time by indicate and simplify Instance.(2/24)
9 * Internal Data Storage (Property). (2/26-28)
1 * CLASS is TYPE (2/16)
In Object Pascal Type begin with "T". In Delphi Class
almost all begin with "T" and originated from Class name
TObject
Example TVAT = Class(TObject).
Writing Class is a real backbone for programmer. Let's say if you
are programming an accounting program, you should write about class
related to finance, costing and so on. You have to write about Class
within Encapsulate between Type and End;
Based on below sample we are dealing with VAT=Valued Added Tax
which could be different based on country, or government policy.
|
Unit UVAT;
Interface
Type
.......TVat = Class
End;
implementation
end.
|
Back to
TOP
2 * Writing Class in style of OOP (2/17)
OOP is a way you write a CLASS with carefully thought out and include
"DATA" and "METHOD". You can always modify or
add features in this Class later on as well. Creating a Class for
a programmer is like an architect creating a blue print before building
a house.
Continuing with our TVat, we should consider all necessary factors
to complete our "Class" here.
|
Unit UVAT;
Interface
Type
.......TVatType = (ExcludeVAT,
IncludeVAT) <enumerate of VatType
include or exclude>
Type
.......TVat = Class
..........Amount : Extended; <variable
store value to be calculate for Vat>
..........VatType : TVatType;
..........VatAmt : Extended; <variable
store value for Vat actual value, could be 6%, 10% or ...>
End;
implementation
end.
|
Back
to TOP
3 * What is Enumerate?(2/18)
A set of commend word is a part of program called "Enumerate".
ExclueVAT in math is Ordinary Cardinal or in brief Ordinal
value =0
IncludeVAT is an Ordinal value =1
We normally use Enumerate when we have stable conditions or factors
in our program such as
|
.......TPassengerSeat = (Economy, Business, FirstClass, VIP, Diplomat)
|
OR
|
.......TTrip = (RoundTrip, OneWay, ConnectFlight)
|
So based this explanation, we cab refer back to out sample why
TVatType is a enumerate.
Back
to TOP
4 * Method declaration of Class (2/19)
A method declaration is a "Pseudo" CLASS which is a plan
of how to do it, and you need to implement your plan by real action
by writing "Method" of how things actually get done.
Sample Below Color match indicate Class connection with Method
|
Unit UVAT;
Interface
Type
.......TVatType = (ExcludeVAT, IncludeVAT)
Type
......TVat
= Class
..........Amount : Extended;
..........VatType : TVatType;
..........VatAmt : Extended;
Procedure CalculatedVAT
(Value:Extended); <this is method
declaration>
End;
implementation
Procedure TVAT.CalculatedVAT
(Value:Extended); <implement
method which belong to TVAT>
.....Begin
.....End;
end.
|
Back
to TOP
5 * Method of calculation sample (2/20)
Before calculation, you have to consider all factors and conditions
of the calculation formula first. For example your client ask you
to write a program with option of include and exclude tax in calculation
possible, then see sample below of our first method of our Class
TVAT.
|
Procedure TVAT.CalculatedVAT (Value:Extended);
.....Begin
..........If VatType = ExcludeVat then
..............VatAmt := Value
* 0.07 <exclude VAT multiple
by 7>
..........else
..............VatAmt :=Value
* (107/100); <Include VAT
multiple by 107 and divide by 100>
..............Amount : = Value;
.....End;
end.
|
Back
to TOP
6 * Testing Class you had created. (sample of
TVAT from 2/20) (2/21)
Assume you are another programmer and another programmer is using
TVAT , go to Main Menu choose File/New Application. In order to
enable our new program to see UVAT we just created, we should add
UVAT in Uses section.
This is what you will see (color indicate relationship)
|
users
.....Windows, Messages, SysUtils,
.....Classes, Graphics, Controls,
.....Forms, Dialogsm, UVAT
;
type .....TForm1 = Class
(TForm)
.....private
............. {private declarations}
.....public
............. {public declarations}
.....end;
var
..........Form1 : TForm1;
..........V1
: TVAT;
implementaion
|
|
Note : V1 is an Instance refer to Class name TVAT
|
Back
to TOP
7 * Instance and CLASS (2/22)
In the old time programmer need to deal with INIT and DESTRUCT variable
which is a "pointer" which point to the "Object"
in the memory and could make simple program turn into a nightmare.
The new age of programmer use "INSTANCE" to call existing
CLASS to make life a little easier. Class itself is an ENTITY, so
we need to instantiated CLASS and turn it into OBJECT, then this
Object is then buried itself in the Hard disk (Stream) waiting to
do what we tell it to do. When we terminate or exit program, this
object will disappear and give back our hard disk resource.
Each instance cost you 4 bytes. If you handle Class, Instance and
your object correctly, you program will NOT be a fat large program.
Back to our sample V1 that refer to Class TVAT, we are now need
to instantiated, create or initialized whatever you want to call
it by using CONSTRUCTOR METHOD "Create".
Doing this ...................... V1
: = TVAT.Create ;
|
V1 : = TVAT.Create;
V1.CalculateVAT(35350.00);
...........
........... ??? V1.VATamt
V1.Free;
|
After Sentence Create; V1 is born. If you try to use V1 before
create, it will caused Memory Error GFP. After V1 is created, then
we can call Class Method name "CalculateVAT and send value
35350.00 to be calculate VatAmt within Method name CalculateVAT.
When finished with this object we will FREE instance, but this instance
will be existing here for next use.
The above may looks normal, but programmer should indicate TYPE
of vat to avoid error, therefore it should be.
|
V1 : = TVAT.Create;
V1.VatType := ExcludeVAT;
V1.CalculateVAT(35350.00);
...........
........... ??? V1.VATamt
V1.Free;
|
Back
to TOP
8 * Save Time by indicate and simplify Instance.(2/24)
Back to UVAT for small changes by indicating "Instance", so that programmer or you do not have to indicate
"Instance" everytime you want to calculate VAT.
Do this changes below
|
Var
........VAT
: TVAT; <add instance
VAT refer o TVAT Class>
implememtation
Procedure TVAT.CalculatedVAT (Value:Extended);
.....Begin
..........If VatType = ExcludeVat then
..............VatAmt := Value * 0.07
..........else
..............VatAmt :=Value * (107/100);
..............Amount : = Value;
.....End;
Initialization <this will be
done before start program>
.............VAT
:= TVAT.Create;
.............VAT.VatType
:= ExcludeVAT;
Finalization <this will be done
before exit program>
.............VAT
.Free;
end.
|
|
Note : Now you don't have to call V1,but call out instance VAT
and followed by Method CalculatedVAT right away.
|
Back
to TOP
9 * Internal Data Storage (Property). (2/26-28)
If you writing program with another programmer and he has to call
"Method" name "CalculatdVat" , or if he could
remember name of "Method" then it could be frustrating
,isn't it? Indicating "Property" value will make thing
easier. You can do this by adding "F" infront Amount and
become FAmount. Next time when you call FAmount , it will automatically
called Method name CalculatedVat. To do this you have to transfer
it to Private Section. Technically, we called this "Internal
data Storage".
Sample of TVAT again
|
Type
........TVAT = Class
.....
.....private
............. FAmount : Extended ;
............. FVatType : TVatType;
..............FVatAmt : : Extended ;
.....public
............. Procedure CalculatedVat (Value:Extended);
..............Property Amount: Extended Read FAmount Write CalculatedVat;
..............Property
VatAmt :Extended Read
FVAtAmt;
.....End;
Var
........VAT : TVAT;
implememtation
Procedure TVAT.CalculatedVAT (Value:Extended);
.....Begin
..........If FVatType = ExcludeVat then ..............
..............FVatAmt := Value * 0.07
..........else
..............FVatAmt :=Value * (107/100);
..............FAmount : = Value;
.....End;
|
|
Note : FvatAmt is in private so nobody will be able to see it,
so allowing internal reading by adding property in public (see RED)
|
Back
to TOP
|