Records
Fundamentals - Class Pair
- What's in a class:
- Define a class for (X, Y) pairs (ie class Pair)
- Declare a variable of class Pair
- Allocate and object of class Pair
- What memory is allocated
- Notice: reference type
- Notice: fields and non-fields
- Can we have fields only?
Fundamentals - Record Type Pair
- Variables of record type contain only fields
Fundamentals - Accessing Fields
- In java
- client code to access and modify Pair fields
- Assume public access modifiers
sop(p.x);
p.y = 3;
In ada
sop(p.x);
p.y = 3;
But, where is type Pair declared?
- Nested in procedure
- In separate package
Context: Classes and Objects
- Java: Classes contain fields (ie instance and class variables)
- An objects is allocated using a class as a type
- Objects (usually) have fields
- Objects are only accessible via references (ie pointers)
- Example
class Pair{
int x, y;
...
}
...
Pair p = new Pair();
Records
- To create and use a record, we must first create a new type
- A record type defines an ordered set of fields
- Each field has a name and a type
- Like java objects and ada arrays, we have separate type declaration
and object allocation
- Fields are accessed with dot notation, similar to java
- Example: tryrecords1.adb
(and prettified)
- Memory allocation: not reference type!
- Like an array, all fields can assigned in one statement
More Working with Records