Structures

In most applications, you will want to associate several data items together. For example, you may want to define a time record that has an integer for each of the following: the hour, the minute, and the second of the specified time. You can declare them like this:

    // start work     int start_sec = 0;     int start_min = 30;     int start_hour = 8;     // end work     int end_sec = 0     int end_min = 0;     int end_hour = 17;

This approach becomes quite cumbersome and error-prone. There is no encapsulation, that is, the _min variables can be used in isolation to the other variables. Does the minutes past the hour make sense when it is used without the hour that it refers to? You can define a structure that associates these items:

 struct time_of_day ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.