STRUCTURE: PUTTING RELATED ITEMS TOGETHER
When writing a program, several variables (identifiers) are declared and used. Some of the names used as identifiers are related and can be grouped together (memory wise) in one location as one unit in memory. This grouping of related names, including several variables and functions is called a structure. In C, structures group only variables, while in C++ structures group variables as well as their related functions. Each variable and function is referred to as a member of the structure. In C++, the bundling of variables and functions adds the important concept of Object Oriented Programming (OOP), also known as class, to the C language. This incorporation of variables and functions makes C++ both a Procedural as well Object-Oriented language.
The key word struct is used to create a structure. In C, there are three general formats to create a structure variable as shown in Figures 7.1a, b, and c. Notice in Figure 7.1a, the structure is defined with the use of a tag name followed by the definition of the structure and the actual structure variable name. In Figure 7.1b, the use of a tag name is eliminated and the rest of the definition and the structure declaration remain the same.






Figure 7.1c shows the C declaration of a structure using the tag name twice. Line one uses the tag name to give the structure a name. Line seven uses the tag name as well as the struct keyword to declare an instance of the structure.
C++ STRUCTURE IS EQUIVALENT TO THE C STRUCTURE
In C, the name after the keyword struct indicates the structure tag that can be used later in the program with the keyword struct to declare a structure variable. However, in C++ the name after the keyword struct is considered a type and can be used to declare a structure variable. Therefore, when a structure variable is declared using C++, the struct keyword is no longer needed. Additionally, C++ structures can include functions as part of the structure. This introduces the concept of OOP and class, which is discussed in a later chapter.
In Figure 7.2a, notice the definition is exactly like that of Figure 7.1a. A structure variable declaration can always follow the definition of a structure in C and C++. In Figure 7.2b, an easier and more meaningful definition and declaration of a structure is given. The C++ declaration uses a tag name followed by the actual structure variable name. The reuse of the struct keyword is eliminated. This elimination leads to less redundancy. It isn’t necessary to retype the struct keyword because with C++ the tag name becomes the type of structure that you are declaring.




The two examples that follow illustrate the differences between C and C++ structures. Figure 7.3a shows the C definition and declaration of a structure and 7.3b shows that of a C++ structure definition and declaration. Both examples define and declare the same type of structure. It is important to observe line 5 of Figure 7.3b, note the use of person, which is the type of structure defined, and is also used to declare an employee structure of type person.
![Text Box: 1. struct persontag{
2. char name[20];
3. double salary;
4. };
5. struct persontag employee;](chapter_7_files/image009.gif)


![Text Box: 1. struct person{
2. char name[20];
3. double salary;
4. };
5. person employee;](chapter_7_files/image009.gif)
C++ STRUCTURE AS A BETTER C STRUCTURE AND AS A CLASS
C++ is known to be superior to C. One reason for its superiority is that C++ simplifies the concepts of the C programming language. Some of these improvements are demonstrated in previous chapters with areas input and output (cin and cout of C++ versus scanf and printf of C) and the passing of parameters (pass by reference of C++ versus pass by pointer of C). Other major improvements are shown in the structure, such as:
1.) In C++, the name after the keyword struct defines the type of structure being defined, therefore, a tag name is not necessary. The use of a tag with C is an overhead particularly when a structure needs to be passed to a function. In C, the keyword typedef is used in conjunction with the structure to overcome this problem.
2.) In C++ a structure can contain functions related to the structure. This makes the structure a class. This transition to class is the gateway to Object-Oriented Programming (OOP). OOP can become tedious to learn but will be beneficial in the long run.
PROGRAMMING WITH STRUCTURES OR WITHOUT: PARALLEL ARRAYS
Programmer uses a structure to group related data under a common name. By doing so, all of the data is treated as one entity. Can you imagine a large program without a structure? A programmer without a structure has to remember, which scattered variables are related to each other. The other alternative is to use an index of parallel arrays to form a relationship between variables. For example name[ i ], phone[ i ], age[ i ] are related by the use of the same index, named i, for all the related variables. Figure 6.19a of Chapter 6 illustrates the use of parallel arrays: such as id[100], hoursworked[100], and hourlyrate[100]. Each value of i represents the ID, hours worked, and the hourly rate of one specific employee.
The examples of Figures 7.4a, b., and c. illustrate how C structures can be used in a program in various ways. Depending on the situation, one style may be preferable. The structures listed in the examples consist of three member variables: name, age, and salary. Each example program reads data into each variable and then displays the variable’s value. The program examples also match the styles used in Figure 7.1a, b, and c respectively. Figure 7.4d shows the output of all three programs based on the same input data.
![Text Box: 1. #include <iostream>
2. using namespace std;
3. struct persontag {
4. char name[ 20 ];
5. int age;
6. float salary;
7. }employee;
8. main( ){
9. cout << " ENTER EMPLOYEE NAME: ";
10. cin >> employee.name;
11. cout << " ENTER EMPLOYEE AGE: ";
12. cin >> employee.age;
13. cout << " ENTER EMPLOYEE SALARY:";
14. cin >> employee.salary;
15. cout << " NAME IS " << employee.name << endl;
16. cout << "AGE IS " << employee.age << endl;
17. cout << " SALARY IS " << employee.salary << endl;
18. return 0;
19. }//MAIN](chapter_7_files/image011.gif)

![Text Box: 1. #include <iostream>
2. using namespace std;
3. struct persontag {
4. char name[ 20 ];
5. int age;
6. float salary; };
7. struct persontag employee;
8. main( ){
9. cout << "ENTER EMPLOYEE NAME: ";
10. cin >> employee.name;
11. cout << "ENTER EMPLOYEE AGE: ";
12. cin >> employee.age;
13. cout << "ENTER EMPLOYEE SALARY:";
14. cin >> employee.salary;
15. cout << " NAME IS " << employee.name << endl;
16. cout << "AGE IS " << employee.age << endl;
17. cout << " SALARY IS " << employee.salary << endl;
18. return 0;
19. }//MAIN](chapter_7_files/image011.gif)



![Text Box: 1. #include <iostream>
2. using namespace std;
3. struct {
4. char name[ 20 ];
5. int age;
6. float salary; }employee;
7. main( ){
8. cout << "ENTER EMPLOYEE NAME: ";
9. cin >> employee.name;
10. cout << "ENTER EMPLOYEE AGE: ";
11. cin >> employee.age;
12. cout << "ENTER EMPLOYEE SALARY:";
13. cin >> employee.salary;
14. cout << " NAME IS " << employee.name << endl;
15. cout << "AGE IS " << employee.age << endl;
16. cout << " SALARY IS " << employee.salary << endl;
17. return 0;
18. }//MAIN](chapter_7_files/image014.gif)

Figures 7.5a. and b. illustrates a C++ structure. Even though a C++ structure has its own style, any C structure shown above can be used in a C++ program. Since C++ is an improved C, it is recommended that you adhere to the C++ structure convention when using structures in a program.
The program examples listed next match the styles used in Figure 7.2a and b respectively. Figure 7.5c shows the output of the two programs based on the same input data.
![Text Box: 1. #include <iostream>
2. using namespace std;
3. struct person {
4. char name[ 20 ];
5. int age;
6. float salary; }employee;
7. main( ){
8. cout << " ENTER EMPLOYEE NAME: ";
9. cin >> employee.name;
10. cout << " ENTER EMPLOYEE AGE: ";
11. cin >> employee.age;
12. cout << " ENTER EMPLOYEE SALARY:";
13. cin >> employee.salary;
14. cout << " NAME IS " << employee.name << endl;
15. cout << " AGE IS " << employee.age << endl;
16. cout << " SALARY IS " << employee.salary << endl;
17. return 0;
18. }//MAIN](chapter_7_files/image015.gif)

![Text Box: 1. #include <iostream>
2. using namespace std;
3. struct person {
4. char name[ 20 ];
5. int age;
6. float salary; };
7. person employee;
8. main( ){
9. cout << " ENTER EMPLOYEE NAME: ";
10. cin >> employee.name;
11. cout << " ENTER EMPLOYEE AGE: ";
12. cin >> employee.age;
13. cout << " ENTER EMPLOYEE SALARY:";
14. cin >> employee.salary;
15. cout << " NAME IS " << employee.name << endl;
16. cout << " AGE IS " << employee.age << endl;
17. cout << " SALARY IS " << employee.salary << endl;
18. return 0;
19. }//MAIN](chapter_7_files/image017.gif)



RECORD: ANOTHER NAME FOR A STRUCTURE
Some languages and databases call the structure a record, each record may contain several fields of data types such as an integer, float, character, bool (Boolean) or other types. The COBOL language frequently uses the concept of a record.
DOT OPERATOR: ACCESS MEMBERS OF A STRUCTURE
To refer to a variable of a structure, use the dot operator (character . period on the keyboard) between the structure name and the structure contained variable. For example, in Figure 7.5a, each of the variables listed below belongs to the structure named person.
· name is a 20 character long character array.
· age is an integer variable
· salary is a float variable
STRUCTURE INITIALIZATION
The data members of a structure can be initialized the following three ways:
1.) At the time of declaration:
![]()
![Text Box: 1. #include <iostream>
2. using namespace std;
3. struct person {
4. char name[ 20 ];
5. int age;
6. float salary; };
7. main( ){
8. person employee = { "Ebrahimi", 35, 350000.00 };
9. cout << " NAME IS " << employee.name << endl;
10. cout << " AGE IS " << employee.age << endl;
11. cout << " SALARY IS " << employee.salary << endl;
12. return 0;
13. }//MAIN](chapter_7_files/image021.gif)
2.)

Manually initializing the members ( You simply assign a value to the members of a structure):
![]()
How do we assign a name to a character array variable named name? There is no problem assigning a single character to a non-character array and adding it to the structure named person:
Then the following line of code would assign the value of capital ‘A’ to the character variable named initial of the structure variable named employee:
![]()
When full names need to be inserted into character arrays, the built-in function named strcpy( ) ( found in the header file #include <string.h>) must be used to copy the value of a name to the name variable. The following line of code accomplishes the task of initializing the name variable of the structure named employee to the value of “Ebrahimi”.
![]()
In C++ the addition of the STL (Standard Template Library) facilitates string assignment. If the library named string (not to be confused with string.h) is added using the following line of code:
![]()
The character array of the person structure is replaced with a string data type declared as follows:
![]()
![]()
Then the following line of code assigns the name of Ebrahimi to the string variable named name:
The programs of Figures 7.6b and c show the manual initialization of a structures data elements. Figure 7.6b shows the initialization using the string.h library and the string copy (strcpy( ) built-in function ) and Figure 7.6c shows the use of the STL’s string
library. We will discuss the STL and strings in more detail in a later chapter.
![Text Box: 1. #include <iostream>
2. #include <string.h> // string library old version
3. using namespace std;
4. struct person{
5. char name[ 20 ];
6. int age;
7. float salary; };
8. main( ){
9. person employee;
10. strcpy( employee.name , "Ebrahimi" );
11. employee.age = 35;
12. employee.salary = 350000;
13. cout << " NAME IS " << employee.name << endl;
14. cout << " AGE IS " << employee.age << endl;
15. cout << " SALARY IS " << employee.salary << endl;
16. return 0;
17. }//MAIN](chapter_7_files/image030.gif)





3.) Through the input routines:
![]()

The program in Figure 7.6e shows the initialization of a structure’s variables using the C++ cin statement. The output is listed in Figure 7.6f.
![Text Box: 1. #include <iostream>
2. using namespace std;
3. struct person {
4. char name[ 20 ];
5. int age;
6. float salary; };
7. person employee;
8.
9. main() {
10. cout << "ENTER EMPLOYEE NAME: ";
11. cin >> employee.name;
12. cout << "ENTER EMPLOYEE AGE: ";
13. cin >> employee.age;
14. cout << "ENTER EMPLOYEE SALARY:";
15. cin >> employee.salary;
16. cout << "NAME IS " << employee.name << endl;
17. cout << "AGE IS " << employee.age << endl;
18. cout << "SALARY IS " << employee.salary << endl;
19. return 0;
20. }//MAIN](chapter_7_files/image037.gif)
![]()

![]()
Several occurrences of the same structure, an array of structures, may be needed to store or retrieve information for a multitude of people. For example, if there are several employees in a department, each employee should have an occurrence of the structure. The index used to move through the array of structures will point to a specific occurrence in the array of structures. In Figure 7.7a, each employee entered has a location, or occurrence, which contains the employee’s name and phone number.
![Text Box: 1. #include< iostream>
2. using namespace std;
3. struct person{
4. char name[ 20 ];
5. char phone[ 16 ]; };
6. main( ){
7. person employee[ 100 ];
8. int n = 0;
9. cout << "Enter an employee name and phone number (Ctrl + Z to end): ";
10. while( cin >> employee[ n ].name >> employee[ n ].phone ) {
11. n++;
12. cout << "Enter an employee name and phone number (Ctrl + Z to end): ";
13. }//WHILE
14. cout << endl << endl << endl;
15. for( int i = 0 ; i < n ; i++ ){
16. cout << "employee.name[" << i << " ] = " << employee[ i ].name << endl;
17. cout << "employee.phone[ " << i << " ] = " << employee[ i ].phone
18. << endl << endl; }//FOR
19. return 0;
20. }//MAIN](chapter_7_files/image041.gif)

![]()
![Text Box: Enter an employee name and phone number (Ctrl + Z to end):
Ebrahimi 1(516)555-1212
Enter an employee name and phone number (Ctrl + Z to end):
Ehrlinger 1(631)555-1212
Enter an employee name and phone number (Ctrl + Z to end):
Smith 1(718)555-1212
Enter an employee name and phone number (Ctrl + Z to end):
Jones 1(212)555-1212
Enter an employee name and phone number (Ctrl + Z to end):
employee.name[ 0 ] = Ebrahimi
employee.phone[ 0 ] = 1(516)555-1212
employee.name[ 1 ] = Ehrlinger
employee.phone[ 1 ] = 1(631)555-1212
employee.name[ 2 ] = Smith
employee.phone[ 2 ] = 1(718)555-1212
employee.name[ 3 ] = Jones
employee.phone[ 3 ] = 1(212)555-1212](chapter_7_files/image044.gif)
ARRAY OF STRUCTURES VERSUS AN ARRAY OF ARRAYS
What is the alternative to an array of structures? A choice is several arrays of arrays. In fact, early languages did not have the ability to create structure arrays. The array of arrays (multi-dimensional array) was a means to represent different but related data types. Disadvantages of an array of arrays versus an array of structures are:
1.) Several variable names and declarations are scattered throughout a program. As a result, the program becomes difficult to read and comprehend.
2.) In order to pass the arrays to functions every array needed must be passed to the function when using an array of arrays. While using an array structure, just the name of the structure is sufficient. Then, within the function, the appropriate data members can be accessed.
POINTER TO A STRUCTURE
It is possible to declare a variable that points to a structure just as an ordinary pointer points to an integer, double or character variable. A pointer to a structure is used as a way to change the value of a structure member variable when it is passed to a function. To access a member variable of a structure using a pointer to the structure, use one of the following syntaxes:

The above two lines accomplish the same task and allow for reading or writing of values from or to the structure member variables. You may ask, “Where did the arrow operator (consisting of a minus sign followed by a greater than sign) come from?”. The arrow operator de-references a pointer so that the value located at the memory address represented by the pointer may be accessed. Since the dot operator has precedence over the de-reference operator ( * ) the pointer named ptr must be de-referenced before the memberdata variable can be accessed. Parentheses force the pointer, named ptr, to be de-referenced first. Then the dot operator can be used to access the memberdata variable. The choice of which syntax to use is based on style; however, the use of the arrow operator reduces keystrokes and gives a less cryptic feel.
Understanding these concepts can be difficult. In a future chapter pointers will be discussed in greater detail.
By default, in C/C++, parameters are passed to a function by value. This means that any change to the value of a parameter's variable will not alter the original value of the variable. In other words, the change to the variable of the called function will not affect the value of the variable of the calling function. Therefore, a structure variable will be passed to a function by value just like any other integer or float variable. When there is a need to change a value or values of a structure data member, C's pass by pointer or C++ pass by reference is used. The program listed in Figure 7.8a shows a simple pass by value of a structure to a function. The output is shown in figure 7.8b.
![]()
![]()

|
employee.name = Ebrahimi
employee.phone = 1(516)555-1212 |
The program listed in Figure 7.8c illustrates C’s pass by pointer while Figure 7.8d illustrates the C++ pass by reference. The identical output based on the same input to both programs is shown in Figure 7.8e.
![Text Box: 1. #include<iostream>
2. #include<string>
3. using namespace std;
4. struct person{
5. char name[ 20 ];
6. char phone[ 16 ]; };
7. // function prototype
8. void printfromstruct( person );
9. main( ){
10. person employee;
11. strcpy( employee.name, "Ebrahimi " );
12. strcpy( employee.phone, "1(516)555-1212 " );
13. printfromstruct( employee );
14. return 0;
15. }//MAIN
16. void printfromstruct( person employee ){
17. // Notice there is no need to pass the person structure by pointer
18. // or by reference. No data is being changed, rather data is just
19. // being printed to the screen.
20. cout << "employee.name = " << employee.name << endl;
21. cout << "employee.phone = " << employee.phone << endl;
22. }//PRINTFROMSTRUCT](chapter_7_files/image047.gif)

![]()

![Text Box: 1. #include<iostream>
2. using namespace std;
3. struct person{
4. char name[ 20 ];
5. char phone[ 16 ]; };
6. // function prototype
7. // The function prototype below tells the compiler that later in the program a
8. // function named readtostruct will be called and defined that will not return a
9. // a value, but expects the memory address of a person structure. The name of
10. // the variable that will be used as the pointer to person does not have to be
11. // defined at this time. The compiler just wants to know that a structure
12. // pointer is coming to the called function.
13. void readtostruct( person* );
14. main( ){
15. person employee;
16. readtostruct( &employee ); // send the memory address of employee
17. cout << "employee.name = " << employee.name << endl;
18. cout << "employee.phone = " << employee.phone << endl;
19. return 0;
20. }//MAIN
21. void readtostruct( person *employee){
22. // Notice the use of the arrow operator( -> ) to access a structures
23. // member when a pointer to a structure is used. Here the parameter
24. // name of the pointer to the person structure must be declared in the
25. // functions header as it will be accessed in the function definition.
26. cout << "Enter an employee name: ";
27. cin >> employee->name;
28. cout << "Enter an employee phone number: ";
29. cin >> employee->phone;
30. }//READTOSTRUCT](chapter_7_files/image050.gif)
![]()

![Text Box: 1. #include<iostream>
2. using namespace std;
3. struct person{
4. char name[ 20 ];
5. char phone[ 16 ]; };
6. // function prototype
7. void readtostruct( person& );
8. main( ){
9. person employee;
10. readtostruct( employee );
11. cout << "employee.name = " << employee.name << endl;
12. cout << "employee.phone = " << employee.phone << endl;
13. return 0;
14. }//MAIN
15. void readtostruct( person &employee){
16. cout << "Enter an employee name: ";
17. cin >> employee.name;
18. cout << "Enter an employee phone number: ";
19. cin >> employee.phone;
20. }//READTOSTRUCT](chapter_7_files/image051.gif)


CHANGING A MEMBER VALUE OF A STRUCTURE IN A FUNCTION
The address of a structure must be sent to a function in order to change the value of a structure’s members. As seen in the prior section, this can be accomplished in two ways: pass by pointer or pass by reference. The programs illustrated in Figures 7.9a and b respectively illustrate this. Once again the output of both programs is identical and displayed in Figure 7.9c.
It is important to note that in both of the following examples, the employee structure member variables are initially set. After the function named changevalues( ) is called, the values held by the employee structure have changed.



![]()


RETURNING A STRUCTURE
A function can return a structure data member or return an entire structure. In the program listed in Figure 7.10a, after data has been assigned to structure member variables, the entire structure is returned to the function main. The output of the program can be seen in Figure 7.10b.
![Text Box: 1. #include<iostream>
2. using namespace std;
3. struct person {
4. char name[ 20 ];
5. int age;
6. float salary; };
7. // function prototype
8. person readtostruct( person );
9.
10. void main( ){
11. person employee;
12. employee = readtostruct( employee );
13. cout << "employee.name = " << employee.name << endl;
14. cout << "employee.age = " << employee.age << endl;
15. cout << "employee.salary = " << employee.salary << endl;
16. }//MAIN
17.
18. person readtostruct( person employee ){
19. cout << "Enter a name: ";
20. cin >> employee.name;
21. cout << "Enter an age for the name: ";
22. cin >> employee.age;
23. cout << "Enter a salary for the name: ";
24. cin >> employee.salary;
25. return employee;
26. }//READTOSTRUCT](chapter_7_files/image056.gif)


![]()
As the name typedef suggests it defines a type from an existing one, or in other words creates an alias. typedef is useful when the C structure is passed to a function. The use of typedef reduces the amount of typing the programmer does.
![Text Box: typedef struct persontag{
char name[20];
double salary;
} person;
person employee;](chapter_7_files/image059.gif)
However, the use of typedef in C++ structures has become limited because the name of the structure is a type in C++ and can be used in the declaration of a structure. Another use of the typedef definition is to replace a lengthy definition or to provide a meaningful name to a type as illustrated below.

ULI replaces the long unsigned int. In the next example, time_t is a shorthand type of a long. Therefore the variable timeseconds can be declared as a type of time_t, which in reality is type long.

EXAMPLE OF C++ STRUCTURE
A C++ structure differs from a C structure in two ways:
1.) The name after the struct keyword can be used for a declaration as shown below.

2.) The structure may contain functions. A brief introduction to functions within structures will come later in the chapter.
There are various structures that are pre-defined and are part of the C libraries that are worth remembering and using. Among the C structures are time and date. Before using these structures within a program, their respective libraries must be included. For example in order to use the time functions, the #include <ctime> header must be included at the top of your source code.
In order to use the time structure, the member variables of the time structure should be understood. Table 7.1 shows the time structure member variables.
|
System Defined Time Structure Variable Members
|
|
|
Member Variable |
Description |
|
tm_sec
|
Holds the second of the time contained by the system clock ( 0 – 59 ). |
|
tm_min
|
Holds the minute of the time contained by the system clock. ( 0 – 59 ). |
|
tm_hour
|
Holds the hour of the time contained by the system clock. ( 0 – 23 ). |
|
tm_mday
|
Holds the current day in the current month ( 1 – days in month ). |
|
tm_wday
|
Holds the number of the day of the week ( 0 – 6, Sunday – Saturday ). The day of the week is zero based. To see the correct day of the week the following would be necessary: tm_wday + 1 |
|
tm_yday
|
Holds the day of year. ( 0 – 364, 365 in leap years ). The day of the year is zero based. If the date was February 1, in order to see the day as the 32nd day of the year the following would be necessary: tm_yday + 1 |
|
tm_mon
|
Holds the current month of the year ( 0 – 11, January – December ). The number of the month is zero based. If the current month were December, in order to see December as the 12th month of the year the following would be necessary: tm_mon + 1 |
|
tm_year
|
Holds the number of years from the year 1900. Therefore if the year was 2002, in order obtain the year 2002, the following would be necessary: tm_year + 1900.
|

The program listed in Figure 7.11 shows an example of how to access the system clock using C/C++.

![]()

STRUCTURE WITHIN STRUCTURE ( NESTED )
A structure may contain one or more other structures. For example you may define a structure called date, which contains another structure called time. The structure date may consist of the month, day, and year. Similarly, the time structure may consist of hour, minutes, and seconds. Figure 7.12a illustrates nested structures. In the example, the time structure is defined within the date structure making the time structure a member variable of the date structure.

![]()


![]()
In the preceding example of Figure 7.12a, notice how a structure member variable is accessed through the use of the dot operator ( a period on the keyboard ). The following line assigns the value of seven to the month member value of the advisingappointment structure:
Next, notice that for the nested structure, defined as time and declared as clock, a dot operator is needed to access the clock member variables. For example, in order to assign an hour to the clock structure nested within the date structure, we must use the advisingappointment structure variable name followed by the dot operator. After doing so, we can access the date structure member variables, which include day, month, year, and the nested structure defined as time, and declared as clock. Therefore, in order to access the member variables of the declared structure named clock, another dot operator must be used followed by the structure variable clock. Finally, access is granted to the three variables of the clock structure which include hour, minutes, and seconds. The following line of code sets the hour variable to 10:
![]()
SELF-REFERENTIAL STRUCTURE
There are times when a structure member variable is used to reference a structure of the same type, this is called a self-referential variable. This concept will be expanded upon in the Chapter on Data Structures later in the book. The next example shows the basics needed to create a dynamic data type called the linked list. The next member variable is a pointer that points to the next node of the same structure.

Union is an aggregate of elements of usually different data types such as int, double, char or even other unions (nested unions). However, unlike the structure, members of the union share the same memory location, that of the largest type of all of the members of the union. The following definition and declaration of a union shows two union member variables named fulltimesalary and parttimehoursworked. During the execution of the program, only one of the variables of the union may hold data.

DIFFERENCE BETWEEN UNION AND STRUCTURE
A union is the same as a structure in that several member variables can be defined. You can think of a union as a structure that doesn’t require all members to be present at all times, only one member is needed. The compiler will set aside the memory for the largest field so that every other field can fit within the one memory location. A union is known as variant record in the Pascal language. Therefore, aside from saving memory, it also helps the programmer realize that only one member will be used at a time. Figure 7.13a shows an example of the use of a union.

![]()
![]()

PUBLIC AND PRIVATE ACCESS OF A STRUCTURE
A key issue to working with a structure in C++ is the accessibility of data to the outside user. By default, access to a member of a structure is public which means that the main program and all other functions can access and change a structure’s member data. The keyword public can be used to explicitly denote public access. However, in order to restrict the access of the structure member to the outside of a structure the keyword private can be used as follows:
![Text Box: struct person {
private:
char name[20];
double salary;
public:
void setname( char [ ] );
char* getname( void );
void setsalary( double );
double getsalary( void );
};](chapter_7_files/image075.gif)
The above structure with the data members name and salary are only accessible by using the member functions setname(), getname(), setsatlary(), and getsalary(). The keyword public preceding the functions in the above structure indicates that the entire program can access the functions. Therefore, any change to the private data has to be done through the structure’s associated functions, and not through direct access of the data through the dot operator.
BUNDLE FUNCTIONS TO DATA MEMBERS
Data and functions can be scattered and declared here and there throughout a program, however, by placing them together we provide meaning and relationship. It would be difficult for a programmer to relate which data goes together and which functions go with the appropriate data. The concept of structures bundle the related data, with the functions necessary, to handle the tasks associated with the data. The example in Figure 7.14a demonstrates the use of the private and public data and functions respectively.