Comp 11

Heterogeneous Aggregate Storage

Today: Defining new types, part I


Problem 3 from before: using parallel arrays

The problem was to write a program to keep track of your transcript. Each entry in the transcript needs to contain:

This data is heterogeneous: For each course, there is an integer course number and floating point numbers for the number of credits and the grade. You could also imagine adding a course name and/or comment field that would use a string.

Since arrays are homogeneous, we had to use separate arrays for the different kind of data, and we elected to use parallel arrays. See transcript.cpp.

Draw a picture.

Wouldn't it be nice if we could store the data in a way that actually keeps the related information together for us?

Introducing struct

A struct is an aggregate data structure, like an array: it glues values together. However, it is heterogeneous and names its elements (C++ calls them member variables).

Just as there are lots of real-world examples of arrays, there are many examples of heterogeneous collections: A wallet might contain paper money, coins, pictures, credit cards. A jewelry box usually has different sized compartments for earings, necklaces, pins, etc. A silverwear tray has different slots for forks, knives, spoons.

We can model many such collections using separate variables (if there is only one wallet, jewelry box, silverwear tray, ...) or parallel arrays (if we have a collection of such things). But both cases fail to model the notion that the contents of a particular wallet all belong together.


Revisiting Transcripts

We can use an array of structs to simplify our transcript program:


But now that we can put information together in a new structure, we can do better. A transcript isn't just an array of course structures it it? What other information is associated with a transcript? What do you want to store? What do you want to do with it?


You're writing a trip planning application, and you want to include airline flights. Define a struct to model airline flights (what are the elements). Imagine you want to be able to search for a particular flight — what do you need?
Mark A. Sheldon (
Last Modified 2017-Sep-07