Review
-
For a single value, use basic data types:
int
,double
,bool
, etc. - Arrays can store sequences of values of the same type; therefore, they are called homogeneous.
- Multidimensional arrays can be used to store data for checkerboards, 2-d games, 3-d graphics, etc, where all data is again of the same type. Consider, for example:
int rubiks_cube[NFACES][NROWS][NCOLS];
This structure can store data for a rubik's cube, where each color is
represented as an integer.
But what if you have data that is not of the same type? This is
called heterogeneous data.
Introducing struct
A struct
is an aggregate data structure,
like an array: it keeps values together. However, a struct
can
store elements of different types – it is thus heterogeneous.
Also, its elements are named – 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 silverware 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, silverware 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.
Example: Tracking Course Information
Let's imagine the following situation. You'd like to keep track of your course information. Each course will have the following information associated with it:
- department name
- course number
- number of credits the course is worth
- whether the course is pass/fail or not
struct
would be perfect!
Miscellaneous struct
information
Allocation and initialization.
To create a variable of a structure type (which will allocate enough memory to hold values for all the members), you can do either of the following:
course c1;
struct course c1;
You may optionally initialize the members of a structure by giving their values in the order the members were declared:
struct course c1 = {"CS", 11, 3.0, false};
Accessing member variables
You can access the members of struct using dot notation:
-
c1.dept
refers to the value of thedept
member variable within the instance of the course struct namedc1
. - The type of
c1.dept
isstring
. - You can get the other elements similarly.
Assignment
-
c1.passfail = true;
changes thepassfail
member variable totrue
. -
c1 = c2;
changes all the values of c1 to be equal to all the values of c2. It's as if you assigned each member variable one at a time.
Other information
struct
? Anything!struct
with array- Array of
struct
s. Very useful! A playlist can be an array of songs, where each song has a name, album, artist, play length. -
struct
containing an array (WARNING!).
struct
differ from a class
?-
Great question! They are actually quite similar. The key difference is that:
in a
struct
, all members are public by default.
Practice Exercise
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?