Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 320 Lecture 23 OO in Ada. OO in Ada(1) Review Questions? Exam 2 next Friday ADTs.

Similar presentations


Presentation on theme: "ITEC 320 Lecture 23 OO in Ada. OO in Ada(1) Review Questions? Exam 2 next Friday ADTs."— Presentation transcript:

1 ITEC 320 Lecture 23 OO in Ada

2 OO in Ada(1) Review Questions? Exam 2 next Friday ADTs

3 OO in Ada(1) Objectives Define OO Look at Ada components Records

4 OO in Ada(1) O_O What do you consider object oriented programming? What language features are necessary? What are the benefits / downsides of it? Examples?

5 OO in Ada(1) Variant records Unions in C/C++ One data structure, choose one of the following variables Example: Payment types –Cash –Check –Credit

6 OO in Ada(1) 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 OO in Ada(1) 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 OO in Ada(1) Uses Stacks –Keep track of all possible transactions –Accounting Types that have common characteristics –What feature in OO is this like?

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

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

11 OO in Ada(1) 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 OO in Ada(1) Other languages Figure out how much memory is needed union mytypes_t { int i; float f; char c; } mytypes; // These all share the same memory location: mytypes.i; mytypes.f; mytypes.c ;

13 OO in Ada(1) 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 OO in Ada(1) 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 OO in Ada(1) 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 OO in Ada(1) 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 OO in Ada(1) 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 OO in Ada(1) 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 OO in Ada(1) Next time Continuing on with OO programming in Ada –Abstract

20 OO in Ada(1) Summary Variant Records


Download ppt "ITEC 320 Lecture 23 OO in Ada. OO in Ada(1) Review Questions? Exam 2 next Friday ADTs."

Similar presentations


Ads by Google