- A record is a structured data type with a fixed number of components. The components may be accessed by name. The components may be heterogeneous.
- A field is a member of a record.
- Think of drivers license.
- Records in c/c++ are implemented as structs.
- Syntax:
struct identifier {
memberlist
};
- And a member list is
-
DataType identifier_list;
-
struct StudentT {
string firstName,
lastName;
char middleInitial;
};
- Please note, this declares a type, it does not allocate memory.
- In some sense it provides a template, so the compiler knows how to build one of these.
- This should be done in the global declaration space at the top.
- It probably should be done before function prototypes, as you will probably want to use structs to pass parameters.
- Remember, int C++ everything must be declared before it is used (or at least partially declared, as we will see later).
- We declare instances of a struct just as we declare instances of any other type.
-
int main() {
float GPA;
StudentT theStudent;
StudentT studentOne,
studentTwo;
- A member selector is an expression use to access components of a struct instance.
- structInstance.fieldName
- theStudent.firstName = "Dan";
- cout &tl;< theStudent.lastName
- Operations on the entire structure are known as Aggregate Operations
- An Aggregate operation is an operation on an entire data structure, as opposed to an operation on an individual component.
- Aggregate operations supported on structures
- NO IO cout << studentOne
- Yes to assignment, studentOne = studentTwo
- No (sort of) to Math, studentOne = studentTwo + theStudent
- No to comparison if (studentOne == studentTwo)
- Yes to pass as parameters
- Yes to return values from functions.
- Structure data should be cohesive.