Lecture 6
- Recap: struct in C/C++.
- Example:
struct CustRec
{
int id;
float balance;
};
- Declare variables of this type:
CustRec c1, c2;
- Declare an array of records of this type:
CustRec c[20];
- Initialize: s1.id = 1;
- Access components. Dot notation.
- Arrays of records. To access, use both [] for accessing the
array elements, and dot, for the components (field) of the
record.
Example: c[2].id
- Records within records (the "party" structure, with an array
of "student" records inside).
- When passed as parameters. IN: value is copied. OUT
(reference): only a pointer (reference) to the record is passed.
- Example: swapping two records. See
swap.cpp, where we have two different
functions for two different types of records.
- Templates in C++.
Motivation: sorting other data types, not just
integers. Instead of writing several separate functions, write only
one "template function" and use it for several types of data.
-
Example of finding the maximum, swaping and sorting other types
of data without rewriting the whole
program. Using templates in C++.
- In-class program reading exercise: the modified swap-testing program
with templates, swapT.cpp. It swaps two elements of
CustRec or Student type.
- In-class exercise, done with Ileana in preparation for Hw 3 (not
finished - doesn't have read/print functions):
sortCR.cpp
- Starting to make the sort function into a templated one:
sortT.cpp
The rest is part of your next Hw 3.
- Preparation for next Homework: Insertion Sort. You will have to implement it
both on integers and as a template (tested on integers and characters).