Download
itec 320 n.
Skip this Video
Loading SlideShow in 5 Seconds..
ITEC 320 PowerPoint Presentation

ITEC 320

205 Vues Download Presentation
Télécharger la présentation

ITEC 320

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. ITEC 320 Lecture 13 OO in Ada

  2. Review • Questions? • HW 3 due tomorrow night

  3. Objectives • Define OO • Look at Ada components • Records

  4. O_O • What do you consider object oriented programming? • What language features are necessary? • What are the benefits / downsides of it? • Examples?

  5. Variant records • Unions in C/C++ • One data structure, choose one of the following variables • Example: Payment types • Cash • Check • Credit

  6. Example type PaymentType is (Cash, Check, Credit); -- The_Type is called the discriminant of the type type Transaction(The_Type: PaymentType := Cash) is record Amount: Integer; case The_Type is when Cash => Discount: boolean; when Check => CheckNumber: Positive; when Credit => CardNumber: String(1..5); Expiration: String(1..5); end case; end record;

  7. Usage t: Transaction; -- Default is cash transaction begin -- All transactions have an amount field put(t.amount); -- Cash transactions have a discount field if t.discount then put("Give a discount"); else put("No discount"); end if; -- Create a new credit transaction t := (credit, 100, "12345", "01/05"); t.amount:= 200; put(t.amount); put(t.CardNumber); put(t.Expiration); put(t.CheckNumber); -- Compiles but raises constraint error --t.The_Type:= check; -- Compile error. --When changing discriminant, the entire record must be assigned.

  8. Uses • Stacks • Keep track of all possible transactions • Accounting • Types that have common characteristics • What feature in OO is this like?

  9. Memory • Consider • What does this say about memory? t := (cash, false); t:= (credit, 100, "12345", "01/05"); t:= (check, 1234);

  10. Dangers • Consider t := (credit, 100, "12345", "01/05"); if t.discount ... end if; t := (cash, false); put(t.CheckNumber);

  11. Safety • Compile time • Type must defined when record created • When type is changed, entire record must be reassigned • Run time • Checking to see if type  field usage

  12. Other languages • Figure out how much memory is needed union mytypes_t { inti; float f; char c; } mytypes; // These all share the same memory location: mytypes.i; mytypes.f; mytypes.c ;

  13. Issues • Still not extendable • No way to create a subtype that allows access to parent’s values the same way it’s values can be accessed • More of a here is an interesting variation on a particular feature

  14. True OO • Tagging – Let Ada know you will be changing it type Object is tagged record X_Coord: Float; Y_Coord: Float; end record; type Point is new Object with null record; type Circle is new Object with record Radius: Float; end record;

  15. Usage • Creation O: Object := (1.0, .5); C: Circle := (0.0, 0.0, 34.7); type Cylinder is new Circle with record Height: Float; end record; Cyl: Cylinder; Cyl := (O with Radius =>41.2, Height =>231.6); Cyl := (C with Height => 231.6);

  16. Points • Existing components are inherited • Once you derive, the parent type cannot be changed, only the child type • Can be converted to an ancestor type, not vice versa (unless untagged)

  17. Designs • Create a base type • Have all your procedures / functions use the base type • Clients can create new types and send the new type cast as an ancestor to the package • Packages can have multiple functions with multiple parameter types (one for each derived / original type)

  18. Pointers • Create a “generic type” that is just a place holder • Derive all of your types from it • Make your data structures work with the “generic type” • One linked list w/ multiple types stored • No need for generics with this method

  19. True OO • Tagging – Let Ada know you will be changing it type Object is tagged record X_Coord: Float; Y_Coord: Float; end record; type Point is new Object with null record; type Circle is new Object with record Radius: Float; end record;

  20. Expanding • Passing records to functions / procedures Before: Procedure Test(testVar: in out testType) After: Procedure Test(testVar: in out testType’Class) What do you think this does?

  21. What to execute? • Have function that takes type from ancestor and one that takes the right type • Both have same name • Which one gets executed? • Specify behavior with overrides overriding – can also be not overriding procedure Operation(X: T);

  22. Expanding • Also applies to pointers Before: type testPtr is access all myClass After: type testPtr is access all myClass’Class type Cell is record Next: access Cell; element: testPtr; end record What would happen if we tagged this?

  23. Lessons • Ada still loves its strict typing • Bends the rules a bit with polymorphism • Or is it rule bending? • Remember the ‘class

  24. Using inheritance • What if you need to tell what object is what type? Checking: Procedure test(P: Person) is if P’Tag = Woman’Tag Or if P in Woman’Class then type Person is abstract tagged record Birth: Date end record; type Man is new Person with record Bearded: Boolean; end record; type Woman is new Person with record Children: Integer; end record;

  25. Review • OO • Principles • Application in Ada