Union in C++
[Link]
Both structure and union are collection of different datatype. They are
used to group number of variables of different type in a single unit. nd
union.
1. Declaration and Initialization of structure starts with struct keyword.
Declaration and Initialization of union starts with union keyword.
2. Structure allocates different memory locations for all its members while
union allocates common memory location for all its members. The
memory occupied by a union will be large enough to hold the largest
member of the union.n declaration
Declaration of union must start with the keyword union followed by the
union name and union's member variables are declared within braces.
Syntax for declaring union
union union-name
{
datatype var1;
datatype var2;
----------
----------
datatype varN;
};
Accessing the union members
We have to create an object of union to access its members. Object is a variable of
type union. Union members are accessed using the dot operator(.) between union's
object and union's member name.
Syntax for creating object of union
union union-name obj;
Example for creating object & accessing union members
#include<iostream.h>
union Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void main()
{
Employee E;
cout << "\nEnter Employee Id : ";
cin >> [Link];
cout << "\nEnter Employee Name : ";
cin >> [Link];
cout << "\nEnter Employee Age : ";
cin >> [Link];
cout << "\nEnter Employee Salary : ";
cin >> [Link];
cout << "\n\nEmployee Id : " << [Link];
cout << "\nEmployee Name : " << [Link];
cout << "\nEmployee Age : " << [Link];
cout << "\nEmployee Salary : " << [Link];
}
Output :
Enter Employee Id : 1
Enter Employee Name : Kumar
Enter Employee Age : 29
Enter Employee Salary : 45000
Employee Id : -20536
Employee Name : ?$?$ ?
Employee Age : -20536
Employee Salary : 45000
In the above example, we can see that values of Id, Name and Age members of
union got corrupted because final value assigned to the variable has occupied the
memory location and this is the reason that the value of salary member is getting
printed very well. Now let's look into the same example once again where we will
use one variable at a time which is the main purpose of having union:
#include<iostream.h>
union Employee
{
int Id;
char Name[25];
int Age;
long Salary;
};
void main()
{
Employee E;
cout << "\nEnter Employee Id : ";
cin >> [Link];
cout << "Employee Id : " << [Link];
cout << "\n\nEnter Employee Name : ";
cin >> [Link];
cout << "Employee Name : " << [Link];
cout << "\n\nEnter Employee Age : ";
cin >> [Link];
cout << "Employee Age : " << [Link];
cout << "\n\nEnter Employee Salary : ";
cin >> [Link];
cout << "Employee Salary : " << [Link];
Output :
Enter Employee Id : 1
Employee Id : 1
Enter Employee Name : Kumar
Employee Name : Kumar
Enter Employee Age : 29
Employee Age : 29
Enter Employee Salary : 45000
Employee Salary : 45000
Here, all the members are getting printed very well because one member is being
used at a time. Example of comparing size of union and
#include<iostream.h>
struct Employee1
{ int Id;
char Name[25];
long Salary;
};
union Employee2
{ int Id;
char Name[25];
long Salary;
};
void main()
{ cout << "\nSize of Employee1 is : " << sizeof(Employee1);
cout << "\nSize of Employee2 is : " << sizeof(Employee2);
Output :
Size of Employee1 is : 31
Size of Employee2 is : 25