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_hour = 8;
int start_min = 30;
int start_sec = 0;
//end work
int end_hour = 17;
int end_min = 30;
int end_sec = 0;
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
{
int hour;
int min;
int sec;
};
Now, let's do my example
In this case, the setw and setfill manipulators are used to set the width of the next inserted item to two characters and to fill any unfilled places with 0. If you want to use setw and setfill, you need to declare its library.
#include <iomanip>
No comments:
Post a Comment