Presentation is loading. Please wait.

Presentation is loading. Please wait.

James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types.

Similar presentations


Presentation on theme: "James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types."— Presentation transcript:

1 James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types.

2 James Tam Declaring Types Why bother? Needed to pass arrays as parameters Creating your own type of variable Making a synonym for an existing type Format: Type Name(1) = Type for name (1); Name(2) = Type for name (2); : : : : Name(n) = Type for name (n);

3 James Tam Declaring Types (2) Example: type Board = array [1..8,1..8] of char; var chessBoard : Board; checkerBoard: Board;

4 James Tam Declaring Types (2) Example: type Board = array [1..8,1..8] of char; var chessBoard : Board; checkerBoard: Board; Declaring the type - defining what the type consists of (creating type) Declaring variables of the new type (creating instances)

5 James Tam Declaring Types (3) Needed to pass arrays as parameters This won't work procedure proc (chessBoard : array of char); begin : end; A specific type needs to be passed in procedure proc (chessBoard : Board); begin : end; Not a type! Programmer-defined type Composite types like arrays cannot be returned from functions

6 James Tam Declaring Types (4) Can be used to provide alternative names for existing types Example: type FloatingPoint = real; var gpa : FloatingPoint; income: real;

7 James Tam Declaring Types (5) Can be used to provide alternative names for existing types Example: type FloatingPoint = real; var gpa : FloatingPoint; income: real; Original type still usable

8 James Tam Where Type Declarations Fit Into Pascal Programs Header const (* Declaration of constants) type (* Declaration of new types *) var (* Declaration of global variables *) (* Declarations of functions & procedures – defining what they do *) Declarations begin : end. Statements

9 James Tam Programmer-Defined Ordinal Types Enumerated types Subrange

10 James Tam Enumerated Types Format: Type Name of type = (identifier 1, identifier 2..identifier n); Example: type Months = (January, February, March, April, May, June, July, August, September, October, November, December); Budget = array [January..December] of real; begin var tamjBudget : Budget; var monthsIndex : Months; for monthsIndex := January to December do tamjBudget[monthsIndex] := 2000 + Random(2000); end.

11 James Tam Operations On Enumerated Types OperationName Equity= Inequity<> Less than< Less than, equal to<= Greater than> Greater than, equal to>= Predecessorpred Successorsucc Ordinal Numberord

12 James Tam Examples Of Operations On Enumerated Types Pred if ((pred(February)) = January) then writeln('Jan comes before Feb'); Ord writeln(ord(January)); (As an ASCII converter) ord('A'); ord (chr(65))

13 James Tam Subrange Used to define a new type which is a subset of an existing type. Syntax: type Subrange name = first value..last value; Example: type Months = (January, February, March, April, May, June, July, August, September, October, November, December); FallTerm = September..December; WinterTerm = January..April;

14 James Tam You Should Now Know Why you need to create your own types How and where programmer defined types are created How to define and declare instances of enumerated types and subranges How to use enumerated types and subranges What operations are valid on enumerated types and how does each one work


Download ppt "James Tam Introduction To Defining New Types in Pascal In this section of notes you how and why programmers can define new types."

Similar presentations


Ads by Google