Discussion of OOP in Ada
Review of Variant Programming
- Example: Transaction Powerpoints
- Create type hierarchy without inheritance
- Discriminant in record
- Case statement in record field options
- Case statement in client determines code options
- Compile time correctness checks
- Run time correctness checks
- Centralized code
- Easy to add ..., harder to add ...
- Similar to C unions
- Share storate locations
OO Characteristics
- State, Behavior, Identity
- OO is easy as PIE
- Abstraction
Type Derivation
-
type height is new float;
- Can add routines, but not fields
Tagged Types
- Example: Transaction Powerpoints
- Allow inheritance (ie adding fields and routines)
- Allow overriding
- Distinguish between type
Transaction
and Transaction'Class
- Example (from Barnes: Safe and Secure Software):
package Geometry is
type Shape is abstract tagged record -- ignoring private for simplicity
x, y: float;
end record;
function area(s: Shape) return float is abstract;
end Geometry;
-- Child package for Circles
package Geometry.Circles is
type Circle is new Shape with record
radius: Float;
end record;
function area(c: Circle) return float; -- Overrides
end Geometry;
package body Geometry.Circles is
function area(c: Circle) return float is
(3.14159 * c.radius ** 2);
end Geometry.Circles;
Code:
Dispatch table
Overriding
- Potential error with overriding functions:
- Misspelled overriding routine (eg aera)
- Incorrect parameters (eg integer instead of float)
- Considered to be new routine instead of overriding
- How to detect:
- Keyword: overriding
- Not required
- Compiler warning: -gnatyO : warn about not marked overriding routine
Avoiding heap allocation
- Array of pointers to circles