Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Fall 2001(c)opyright Brent M. Dingle 2001 Known Data Types So far we have seen several predefined data types in Pascal: char integer real boolean others But what if we want to represent something that is none of those?
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types Notice that while we understand how to use types char, int, real and boolean we don’t know how things “really” work. A data type consists of a set of values together with a set of operations defined on these values. A data type is called an abstract data type if the programmer who is using the type does not have access to the details of how the operations are implemented (or does not (re)write them for the program in which they are used).
Fall 2001(c)opyright Brent M. Dingle 2001 TYPE Declaration and format In Pascal the TYPE declaration allows you to declare new data types. Type declarations are of the format: TYPE [type_name 1] = [type definition 1]; [type_name 2] = [type definition 2]; : : : : [type_name n] = [type definition n];
Fall 2001(c)opyright Brent M. Dingle 2001 Order of Declarations So in a program the order of declarations becomes: 1. Constants (CONST) 2. Types (TYPE) 3. Variables (VAR) 4. Procedures and Functions
Fall 2001(c)opyright Brent M. Dingle 2001 Program Layout And the program layout becomes Program [Name]; CONST { constants declared here } TYPE { types declared here } VAR { variables declared here } { Procedures and Functions declared here } BEGIN [main body] END.
Fall 2001(c)opyright Brent M. Dingle 2001 Subrange Types The simplest ‘new’ type is a subrange. Subranges are intervals of already defined types in which the type members can be counted. An example of a valid subrange is: the numbers from 4 to 10 are a subrange of integers. i.e. TYPE SHORT_RANGE = Note: you canNOT have a subrange of things like type real – reals are not a countable set.
Fall 2001(c)opyright Brent M. Dingle 2001 Subrange Type (cont) Say you knew, for some reason, that you only wanted to use the numbers from –5 to 15 and you needed a variable named score to handle this. You would declare things as follows: PROGRAM la; TYPE ODD_RANGE = ; VAR odd_score : ODD_RANGE; score : integer; :
Fall 2001(c)opyright Brent M. Dingle 2001 Subrange – host type In the above example type integer would be the host type of the subrange ODD_RANGE.
Fall 2001(c)opyright Brent M. Dingle 2001 Subrange – compatibility A variable of the host type may be assigned a value of the subrange type. e.g. score := odd_score; is a valid statement A subrange type should NOT be assigned a value outside its range. e.g. odd_score := 342; is NOT a valid statement Caution: odd_score := score; will compile but if score’s value is not in [-5, 15] an error may unexpectedly occur.
Fall 2001(c)opyright Brent M. Dingle 2001 Subrange – Another Example You can also do subranges on char- the number of chars is countable. For example all capital letters: PROGRAM la; TYPE CAP_LETTERS = ‘A’..’Z’; VAR letter: CAP_LETTERS; any_char: char;
Fall 2001(c)opyright Brent M. Dingle 2001 Task (not graded) Read the case study on page 296 – 305 Understand how they broke the task into subtasks and how they created their algorithm and how they implemented their algorithm using ADT’s, procedures, functions and units. Try entering the program and running it.
Fall 2001(c)opyright Brent M. Dingle 2001 Ordinal Types Types whose values can be specified by a list are ordinal types. These have also been called scalar types and defined as a data type containing a finite number of elements. Examples: integer char boolean PRIME_COLORS = (red, green, blue)
Fall 2001(c)opyright Brent M. Dingle 2001 Enumerated Types An enumerated type is just a list of identifiers. Enumerated types are user defined ordinal types. The values of an enumerated type have no properties other than their order and their names Their proper use can make programs easier to read.
Fall 2001(c)opyright Brent M. Dingle 2001 Enumerated Type (example) PROGRAM Rainbow; TYPE COLORS = (Red, Orange, Yellow, Green, Blue, Indigo, Violet); VAR color : COLORS; BEGIN color := Yellow; color := Violet; IF (color = Violet) THEN BEGIN Writeln(‘Color is violet.’); END; END.
Fall 2001(c)opyright Brent M. Dingle 2001 Type Casting Notice since there are 7 colors you can type cast the integers 0 to 6 to a color. For example if you said: color := COLORS(2); color would be set = yellow. color := COLORS(0); sets color = red. color := COLORS(42); produces an error as 42 is out of the range of COLORS
Fall 2001(c)opyright Brent M. Dingle 2001 Pred and Succ Functions Remember the functions Pred and Succ? Since Enumerated types have an order, these functions automatically work on them. Some things to watch out for: if you try to do succ(violet) you should get a compile time error – out of range. If you ‘accidentally’ try to take the succ(color) when color’s value is violet you may get an unexpected result at runtime.
Fall 2001(c)opyright Brent M. Dingle 2001 Ord and Chr Ord will work and can be useful on enumerated types. Chr really doesn’t do much for enumerated types, though when converting from type char to type integer and back again it comes in useful.
Fall 2001(c)opyright Brent M. Dingle 2001 Problems (not graded) page , 12, 13 pages 320 – 323, programming 16, 20, 21, 22 Also suggested would be to look at the sample programs: Rainbow.pas Rainbow2.pas
Fall 2001(c)opyright Brent M. Dingle 2001 End ADTs Chapter 8.2, 8.3 now complete. And so ends Chapter 8