270 likes | 493 Views
ITEC 320. Lecture 13 OO in Ada. Review. Questions? HW 3 due tomorrow night. Objectives. Define OO Look at Ada components Records. O_O. What do you consider object oriented programming? What language features are necessary? What are the benefits / downsides of it? Examples?.
E N D
ITEC 320 Lecture 13 OO in Ada
Review • Questions? • HW 3 due tomorrow night
Objectives • Define OO • Look at Ada components • Records
O_O • What do you consider object oriented programming? • What language features are necessary? • What are the benefits / downsides of it? • Examples?
Variant records • Unions in C/C++ • One data structure, choose one of the following variables • Example: Payment types • Cash • Check • Credit
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;
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.
Uses • Stacks • Keep track of all possible transactions • Accounting • Types that have common characteristics • What feature in OO is this like?
Memory • Consider • What does this say about memory? t := (cash, false); t:= (credit, 100, "12345", "01/05"); t:= (check, 1234);
Dangers • Consider t := (credit, 100, "12345", "01/05"); if t.discount ... end if; t := (cash, false); put(t.CheckNumber);
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
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 ;
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
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;
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);
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)
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)
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
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;
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?
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);
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?
Lessons • Ada still loves its strict typing • Bends the rules a bit with polymorphism • Or is it rule bending? • Remember the ‘class
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;
Review • OO • Principles • Application in Ada