In C++, records are called structures. We can use the reserved word struct to define a record data type and declare a record type variable:
struct StudentRecord {
// Defining a record
data type to store student's info char student_no[8]; // 1st field: Student Number char name[20]; // 2nd field: Name char class_name[3]; // 3rd field: Class int class_no; // 4th field: Class Number }; struct StaffRecord { // Defining a record data type to store staff's info char employee_no[6]; // 1st field: Employee Number float salary; // 2nd field: Salary }; struct StudentRecord a_student; // a_student is a student record variable struct StaffRecord an_employee; // an_employee is a staff record variable |
Usually, we use typedef to define a record data type for convenience:
C++ version | Equivalent Pascal version |
typedef struct StudentRecord { char student_no[8]; char name[20]; char class_name[3]; int class_no; } StudRec; typedef struct StaffRecord { char employee_no[6]; float salary; } StaffRec; StudRec a_student; StaffRec an_employee; |
type StudRec = record student_no: string[8]; name: string[20]; class_name: string[3]; class_no: integer end; StaffRec = record employee_no: string[6]; salary: real; end; var a_student: StudRec; an_employee: StaffRec; |
The syntax for referring a field of a record in C++ is the same as that in Pascal: using a dot (.). For example, according to the above record definitions, an_employee.salary refers to the field salary of the record an_employee.
Declaring an array of record is similar to declaring an array of simple data types. For example, according to the above record definitions, the following statement declares two arrays of records:
StudRec students[1500]; // an
array of 1500 student records StaffRec staff[100]; // an array of 100 staff records |
The following sample program outputs all students with surname "Chan":
C++ version | Pascal version |
#include <iostream.h> #include <string.h> #define MAX 5 main() { typedef struct StudentRecord { char student_no[8]; char name[20]; } StudRec; StudRec students[MAX]; int i; char s[20]; for (i = 0; i < MAX; i++) { cout << "Student " << i << ":\n"; cout << "Student Number: "; cin.getline(students[i].student_no, 8); // The above statement reads a string // of maximum length 8 into the variable // students[i].student_no. cout << "Name: "; cin.getline(students[i].name, 20); } for (i = 0; i < MAX; i++) { strncpy(s, students[i].name, 4); s[4] = '\0'; if (strcmp(s, "Chan") == 0) cout << students[i].name << endl; } return 0; } |
program prog_27(input, output); const MAX = 5; type StudRec = record student_no: string[8]; name: string[20] end; var students: array[0..MAX - 1] of StudRec; i: integer; s: string[20]; begin for i := 0 to MAX - 1 do begin writeln('Student ', i, ':'); write('Student Number: '); readln(students[i].student_no); write('Name: '); readln(students[i].name) end; for i := 0 to MAX - 1 do if copy(students[i].name, 1, 4) = 'Chan' then writeln(students[i].name) end. |
Sample output (text in red is entered by user): | |
Student 0: Student Number: 123456 Name: Chan Chi Ming Student 1: Student Number: 475721 Name: Lam Wai Ming Student 2: Student Number: 134578 Name: Chan Kin Wing Student 3: Student Number: 721535 Name: Chan Lai Sheung Student 4: Student Number: 127310 Name: Man Chi Wah Chan Chi Ming Chan Kin Wing Chan Lai Sheung |