1 / 12

Introduction To Defining New Types in Pascal

Introduction To Defining New Types in Pascal. In this section of notes you how and why programmers can define new types. Declaring Types. Why bother? Creating your own type of variable Making a synonym for an existing type Format: Type Name(1) = Type for name (1) ;

avon
Download Presentation

Introduction To Defining New Types in Pascal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Declaring Types • Why bother? • 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. Declaring Types (2) • Can be used to provide alternative names for existing types • Example: • type • FloatingPoint = real; • var • gpa : FloatingPoint; • income: real;

  4. Declaring Types (2) • Can be used to provide alternative names for existing types • Example: • type • FloatingPoint = real; • var • gpa : FloatingPoint; • income: real; Declaring the type - defining what the type consists of (creating type) Declaring a variable of the new type (creating instances)

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

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

  7. Programmer-Defined Ordinal Types • Enumerated types • Subrange

  8. 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.

  9. Operations On Enumerated Types

  10. 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) writeln(ord('A')); writeln(ord (chr(65))); writeln(chr(65));

  11. 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;

  12. 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

More Related