Introduction
Data types encountered thus far are basic data types. These are:
- Integer
- Real
- Character
- Boolean
- String
Real world situations require the relating of basic data type to form a structured data type. i.e. Complex data types made up of existing data types.
Such structures of data may not necessarily exist in isolation. The operations on these data types have to be specified into methods bringing on the concept of objects (A collection of data items with their associated methods for manipulating the data items).
Records
Designing Records
A record type definition will have fields with each field having an identifier. These identifiers and their associated data types determine the record strucure. The fields and their identifiers form the record variable. Example below:
MyRecord Name Stuart Banner Address 99 Some Street, Somewhere PostCode MT2 2OU Gender M Age 21 TeleNo 01234 567890
| Structure definition. PeopleType | ||
|---|---|---|
| Type | Field Identifier | Description |
| String | Name | First name and surname |
| String | Address | Address to town level |
| String | PostCode | Postcode in standard format |
| Character | Gender | Gender 'M' or 'F' |
| Integer | Age | Age at last birthday |
| String | TeleNo | Telephone number in standard format |
The above is a type definition or, in C++, structure definition.
| Data table for variables of a record type. | ||
|---|---|---|
| Type | Identifier | Description |
| PeopleType | MyRecord | Record variable of the defined type |
| PeopleType | AnotherRecord | Another record variable of the defined type |
Accessing Records
Accessing record fields is done by dot notation:
MyRecord.Name
Assigning a value to a string
MyRecord.Name <- "John Smith"
Assigning a value to an integer by increment
MyRecord.Age <- MyRecord.Age + 1
Initialising a record variable
MyRecord.Name <- "John Smith" MyRecord.Address <- "99 Sum Street, Blogsville" MyRecord.Postcode <- "AA1 9ZZ" MyRecord.Gender <- "M" MyRecord.Age <- MyRecord.Age + 1 MyRecord.Name <- "01234 567890"
Where two record variables declared of the exact same type, assignment is allowed:
YourRecord <- MyRecord
When the value of one record hasa been assigned to another, the two records hold the same values in their fields. They are said to be equal and the equality and not equal operators can be applied to records.
Records in C++
Programmer defined record types given by structure definitions are only available in programs where the record type has been defined. In C++ this is achieved by use of the keyword struct:
struct PeopleType
{
AnsiString Name;
AnsiString Address;
AnsiString PostCode;
char Gender;
int Age;
AnsiString TeleNo;
};
The semi-colon after the final brace indicates to the compiler that no variable declarations are to be included with the definition. Course style dictates that variable definitions follow the usual style:
PeopleType MyRec; PeopleType YourRec;
Comments, suggestions, ideas to
Stuart Banner
