Union in C++ with examples

In this tutorial, we will learn what is a union in C++ and its use when compared with structure. We will understand it using a few examples in C++.

Union in C++

A union is a user-defined data type in C++.

Syntax for union declaration:

union union_name
{
    datatype1 var_name1;
    datatype2 var_name2;
    .
    .
    datatypen var_namen;
};

For accessing the members of a union we need to create a union object. We can create union objects in two ways.

  1. union union_name union_object_name;
  2. during declaration:
    union union_name
    {
    //data members
    }union_object_name1, union_object_name2, …union_object_namen;

Example 1: Union

#include<iostream>
using namespace std;

union product
{
  int productid;
  char name[20];
  float price;
};

int main()
{
  union product obj;
  cout << "Enter product-id: ";
  cin >> obj.productid;
  cout << "Enter name of product: ";
  cin >> obj.name;
  cout << "Enter price of product: ";
  cin >> obj.price;
  cout << "Product-id is: " << obj.productid << endl;
  cout << "Product name is: " << obj.name << endl;
  cout << "Product price is: " << obj.price;
  return 0;
}

A union can have many members but only one of its members will have a value at any given time.

Here, we stored the last value in the price. So, only the price variable will have the correct value.

Output:

Enter product-id: 12
Enter name of product: Phone
Enter price of product: 1200.89
Product-id is: 1150688379
Product name is: {ûDe
Product price is: 1200.89

Example 2: Union in C++ (Manipulate the value)

In the above example, there was a problem because we are getting incorrect values except for the last stored element. This is because we are manipulating them after accepting all the members. In a union, only one of its members will have a value. So, we need to manipulate the value immediately before accepting another data member.

#include<iostream>
using namespace std;

union product
{
  int productid;
  char name[20];
  float price;
};

int main()
{
  union product obj;
  cout << "Enter product-id: ";
  cin >> obj.productid;
  cout << "Product-id is: " << obj.productid << endl;
  cout << "Enter name of product: ";
  cin >> obj.name;
  cout << "Product name is: " << obj.name << endl;
  cout << "Enter price of product: ";
  cin >> obj.price;
  cout << "Product price is: " << obj.price;
  return 0;
}

Output:

Enter product-id: 12
Product-id is: 12
Enter name of product: Phone
Product name is: Phone
Enter price of product: 1200.89
Product price is: 1200.89

Example 3:

You might now wonder what is the use of union. Let us compare it with structure and see.

#include<iostream>
using namespace std;

union union_product
{
  int productid;
  char name[20];
  float price;
};

struct struct_product
{
  int productid;
  char name[20];
  float price;
};

int main()
{
  union union_product union_obj;
  struct struct_product struct_obj;
  cout << "Size of union object: " << sizeof(union_obj) << endl;
  cout << "Size of structure object: " << sizeof(struct_obj);
  return 0;
}

Output:

Size of union object: 20
Size of structure object: 28

If we observe the output, we can clearly see that the size occupied by the union object (20=max(4,20,4)) is less than the size occupied by the structure object(28=4+20+4). A union will have the memory size which is sufficient to hold its largest data member.

A union is useful in situations where immediate manipulations are done before storing a value in another data member. In these situations, it is memory efficient when compared to structures.

You may also read,
Structures in C++
Inline Function in C++ with example

Leave a Reply

Your email address will not be published. Required fields are marked *