Example of Union in C++

C++




// C++ program to illustrate the use of union in C++
#include <iostream>
using namespace std;
  
// Creating a union
union geek {
    // Defining data members
    int age;
    char grade;
    float GPA;
};
  
int main()
{
  
    // Defining a union variable
    union geek student1;
  
    // Assigning values to data member of union geek and
    // printing the values of data members
    student1.age = 25;
    cout << "Age : " << student1.age << endl;
  
    student1.grade = 'B';
    cout << "Grade : " << student1.grade << endl;
  
    student1.GPA = 4.5;
    cout << "GPA : " << student1.GPA << endl;
  
    return 0;
}


Output

Age : 25
Grade : B
GPA : 4.5

We have not assigned the values to all data members in one go as we do normally because they share the same memory location and if we do so then their value will be overwritten.

C++ Program to Illustrate that Data Members of Union Share Same Memory Location

In the below example, we are verifying that is it actually data members of the union are sharing the same memory or not.

C++




#include <iostream>
using namespace std;
  
// Creating a union
union geek {
    // Defining data members
    int age;
    float GPA;
    double marks;
};
  
int main()
{
  
    // Defining a union variable.
    geek student1;
  
    cout << "Memory address of age: " << &student1.age
         << endl;
    cout << "Memory address of GPA: " << &student1.GPA
         << endl;
    cout << "Memory address of marks: " << &student1.marks
         << endl;
  
    cout << "Size of a union: " << sizeof(student1) << endl;
  
    return 0;
}


Output

Memory address of age: 0x7ffcfc68fc20
Memory address of GPA: 0x7ffcfc68fc20
Memory address of marks: 0x7ffcfc68fc20
Size of a union: 8

Explanation

First, we have defined a union and defined three different data members inside it. After that, in the main function, we have defined a union variable. Now, we have printed the address of all data members of the union using the reference operator ‘&’.

We can see in the output that the addresses of all three data members are the same which proves that members of the union shared the same memory. We have also printed the size of a union which is ‘8’ because double takes 8 bytes and it largest among int, float, and double.

C++ Unions

In C++, a union is a user-defined datatype in which we can define members of different types of data types just like structures. But one thing that makes it different from structures is that the member variables in a union share the same memory location, unlike a structure that allocates memory separately for each member variable. The size of the union is equal to the size of the largest data type.

Memory space can be used by one member variable at one point in time, which means if we assign value to one member variable, it will automatically deallocate the other member variable stored in the memory which will lead to loss of data.

Similar Reads

Need of Union in C++

When the available memory is limited, it can be used to achieve memory efficiency. It is used to encapsulate different types of data members. It helps in optimizing the performance of applications....

Syntax of Union in C++

Union is defined using the ‘union’ keyword....

Example of Union in C++

C++ // C++ program to illustrate the use of union in C++ #include using namespace std;    // Creating a union union geek {     // Defining data members     int age;     char grade;     float GPA; };    int main() {        // Defining a union variable     union geek student1;        // Assigning values to data member of union geek and     // printing the values of data members     student1.age = 25;     cout << "Age : " << student1.age << endl;        student1.grade = 'B';     cout << "Grade : " << student1.grade << endl;        student1.GPA = 4.5;     cout << "GPA : " << student1.GPA << endl;        return 0; }...

Anonymous Unions in C++

...

Union-like Classes in C++

...

Contact Us