25
ITEC 320 Lecture 13 OO in Ada

ITEC 320 Lecture 13 OO in Ada. OO in Ada(1) Review Questions? HW 3 due tomorrow night

Embed Size (px)

Citation preview

ITEC 320

Lecture 13OO in Ada

OO in Ada(1)

Review

• Questions?• HW 3 due tomorrow night

OO in Ada(1)

Objectives

• Define OO• Look at Ada components• Records

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?

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

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;

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.

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?

OO in Ada(1)

Memory

• Consider

• What does this say about memory?

t := (cash, false); t := (credit, 100, "12345", "01/05");t := (check, 1234);

OO in Ada(1)

Dangers

• Considert := (credit, 100, "12345", "01/05"); if t.discount

... end if; t := (cash, false); put(t.CheckNumber);

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

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 ;

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

OO in Ada(1)

True OO

• Tagging – Let Ada know you will be changing ittype Object is taggedrecord

X_Coord: Float;Y_Coord: Float;

end record;

type Point is new Object with null record;

type Circle is new Object withrecord

Radius: Float;end record;

OO in Ada(1)

Usage

• Creation

O: Object := (1.0, .5);C: Circle := (0.0, 0.0, 34.7);

type Cylinder is new Circle withrecord

Height: Float;end record;Cyl: Cylinder;Cyl := (O with Radius =>41.2, Height =>231.6);Cyl := (C with Height => 231.6);

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)

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)

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

OO in Ada(1)

True OO

• Tagging – Let Ada know you will be changing ittype Object is taggedrecord

X_Coord: Float;Y_Coord: Float;

end record;

type Point is new Object with null record;

type Circle is new Object withrecord

Radius: Float;end record;

OO in Ada(1)

Expanding

• Passing records to functions / proceduresBefore:Procedure Test(testVar: in out testType)

After:Procedure Test(testVar: in out testType’Class)

What do you thinkthis does?

OO in Ada(1)

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 overridingprocedure Operation(X: T);

OO in Ada(1)

Expanding

• Also applies to pointers

Before:type testPtr is access all myClass

After:type testPtr is access all myClass’Class

type Cell isrecord

Next: access Cell;element: testPtr;

end record

What would happen if we tagged this?

OO in Ada(1)

Lessons

• Ada still loves its strict typing• Bends the rules a bit with

polymorphism– Or is it rule bending?

• Remember the ‘class

OO in Ada(1)

Using inheritance

• What if you need to tell what object is what type?

type Person is abstract taggedrecord

Birth: Dateend record;type Man is new Person withrecord

Bearded: Boolean;end record;type Woman is new Person withrecord

Children: Integer;end record;

Checking:Procedure test(P: Person) isif P’Tag = Woman’TagOrif P in Woman’Class then

OO in Ada(1)

Review

• OO– Principles– Application in Ada