6 User Defined Data Types Abstract Data Types
6. User Defined Data Types & Abstract Data Types Mark Dixon, School of Computing SOFT 120 Page 1
Example Problem: Employee Data • Need to keep a record of employee details – e. g. • • • surname forenames date of birth address telephone number salary Mark Dixon, School of Computing SOFT 120 Page 2
User Interface Design • Must respond to following events: • • Click Quit button: closes application Click Previous button: move to previous employee’s details Click Next button: move to next employee’s details Type in fields: change current employee’s details Mark Dixon, School of Computing SOFT 120 Page 3
Code Design Event Handlers Form Create cmd. Next Click cmd. Previous Click General Procedures Employee Display Employee Store cmd. Quit Click Mark Dixon, School of Computing SOFT 120 Page 4
Data Design • We could use an array for each piece of employee information Surnames: string Forenames: string Salaries: double 1 1 1 5 5 5 10 10 10 Mark Dixon, School of Computing SOFT 120 Page 5
Data Implementation var Surnames: array[1. . 10] of string; var Forenames: array[1. . 10] of string; var Salaries: array[1. . 10] of double; var cur. Emp: integer; Mark Dixon, School of Computing SOFT 120 Page 6
Employee Display Procedure procedure Emp. Display(); begin with frm. Employees do begin lbl. Emp. Num. Caption : = Int. To. Str(cur. Emp); txt. Surname. Text : = Surnames[cur. Emp]; txt. Forenames. Text : = Forenames[cur. Emp]; txt. Salary. Text : = Float. To. Str(Salaries[cur. Emp]); end; Mark Dixon, School of Computing SOFT 120 Page 7
Employee Store Procedure procedure Emp. Store(); begin with frm. Employees do begin Surnames[cur. Emp] : = txt. Surname. Text; Forenames[cur. Emp] : = txt. Forenames. Text; Salaries[cur. Emp] : = Str. To. Float(txt. Salary. Text); end; Mark Dixon, School of Computing SOFT 120 Page 8
Form Create and Quit procedure Tfrm. Employees. Form. Create(Sender: TObject); begin cur. Emp : = 1; Emp. Display(); end; procedure Tfrm. Employees. cmd. Quit. Click(Sender: TObject); begin Application. Terminate(); end; Mark Dixon, School of Computing SOFT 120 Page 9
Previous and Next Employee procedure Tfrm. Employees. cmd. Previous. Click(Sender: TObject); begin if cur. Emp > 1 then begin Emp. Store(); cur. Emp : = cur. Emp -1; Emp. Display(); end; procedure Tfrm. Employees. cmd. Next. Click(Sender: TObject); begin if cur. Emp < 10 then begin Emp. Store(); cur. Emp : = cur. Emp + 1; Emp. Display(); end; Mark Dixon, School of Computing SOFT 120 Page 10
Difficulty • This design works • However, if – all fields were implemented, and – more complex operations were added • the code would become difficult to manage – having several separate arrays • Arrays allow data to be grouped – However, arrays must be homogenous (the same type) • it would be useful to be able to group different (heterogeneous) types of data Mark Dixon, School of Computing SOFT 120 Page 11
User Defined Data Types: Records • Groups different types of data • Declaration of type: type TAnimal. Rec = record Name: string; Species: string; Gender: Boolean; end; • Use of type (in variable declaration): var My. Pet: TAnimal. Rec; Mark Dixon, School of Computing SOFT 120 Page 12
Use of type • Change value of My. Pet’s name: My. Pet. Name : = ‘George’; • Can also have arrays of user defined types: var My. Pets: array[1. . 5] of TAnimal. Rec; • Change value: My. Pets[3]. Name : = ‘George’; Mark Dixon, School of Computing SOFT 120 Page 13
Data Design 2 • We can now use a single array that uses a user defined type/record: Employees: TEmployee 1 5 10 Surnames: string Forenames: string Salaries: double • makes it easier to get details of single employee Mark Dixon, School of Computing SOFT 120 Page 14
Data Implementation 2 type TEmployee = record Surname: string; Forenames: string; Salary: double; end; var Employees: array[1. . 10] of TEmployee; var cur. Emp: integer; Mark Dixon, School of Computing SOFT 120 Page 15
Employee Display Procedure 2 procedure Emp. Display(); begin with frm. Employees do begin lbl. Emp. Num. Caption : = Int. To. Str(cur. Emp); txt. Surname. Text : = Employees[cur. Emp]. Surname; txt. Forenames. Text : = Employees[cur. Emp]. Forenames; txt. Salary. Text : = Float. To. Str(Employees[cur. Emp]. Salary); end; Mark Dixon, School of Computing SOFT 120 Page 16
Employee Store Procedure 2 procedure Emp. Store(); begin with frm. Employees do begin Employees[cur. Emp]. Surname : = txt. Surname. Text; Employees[cur. Emp]. Forenames : = txt. Forenames. Text; Employees[cur. Emp]. Salary : = Str. To. Float(txt. Salary. Text); end; Mark Dixon, School of Computing SOFT 120 Page 17
Abstract Data Types • Abstract Data Type = User Defined Data Type + Operations performed on it • Example: – Employee User Defined Data Type – Operations • Employee Display • Employee Store Mark Dixon, School of Computing SOFT 120 Page 18
- Slides: 18