Records
A record is a compound data struct consisting of multiple types of data. Each record is broken up into individual fields (members)
Defining Structure Types
- Define a new structure type
- Specifies types and names of fields
- Declare variables of the new type
Syntax for defining a struct type:
struct NAME {
FIELD_DECLARATIONS;
};
Example:
struct employeeRecordT {
string name;
string title;
string ssnum;
double salary;
int withholding;
};
Declaring Struct Variables:
employeeRecordT empRec;
Record selection and assignment:
empRec.title = "Name"
Record initialization: Values should be the same order as in definition
employeeRecordT manager = {
"Ebenezer Scrooge", "Partner", "271-82-8183", 250.00, 1
};
Pointers to Records
Larger structured data are often declared to be pointers to records.
// Declaring a pointer of the type employeeRecordT
employeeRecordT *empPtr;
// Creating a record variable
employeeRecordT empRec;
// Assigning the address to the pointer
empPtr = &empRec;
The dynamic approach:
employeeRecordT *manager = new employeeRecordT
{ "Ebenezer Scrooge",
"Partner",
"271-82-8183",
250.00,
1
};
Refering to fields:
// Some issues with operator precedence
(*empPtr).salary;
// Shorthand
empPtr->salary;
Creating a list of records (dynamic approach) and initialization (C++11)
- For less modern approach use pointer expressions
employeeRecordT *manager = new employeeRecordT[2] {
{ "Ebenezer Scrooge",
"Partner",
"271-82-8183",
250.00,
1 },
{ "Name2",
"...",
"114-514-1919",
114.514,
2 }
};
Record Member Functions
Records can have member functions. The only difference between records and classes is that records only have public
sections.
Record Constructors
Records can have constructor functions, and the definition approach is just the same as in class definition.
They can also have destructors.
Syntax:
struct Rectangle {
int width; // member variable
int height; // member variable
// constructors
Rectangle()
{
width = 1;
height = 1;
}
Rectangle( int width_ )
{
width = width_;
height = width_ ;
}
Rectangle( int width_ , int height_ )
{
width = width_;
height = height_;
}
// ...
};
Initialize with constructor:
Rectangle rec(3, 2);