UNION in C++

What is the definition of Union in C++?

The union is a collection of multiple data elements that can be of different data types. But, only one of these data items can be stored in the union variable.
the memory space required to store one variable of union is equal to the memory space required by the largest element in the union.

  • A union(defined with keyword union) is a region of memory that, over time, can contain objects of a variety of types
  • A union can contain a maximum of one object because of the member of a union share the same storage space
  • Using unions conserves storage

The result of referencing a union member other than the last one stored is undefined.It treats the stored data as a different type

If data are stored in a union as one type and referenced as another type, the results are implementation-dependent

syntax of union is:

union union_name
{
data_type variable_name;
data_type variable_name;
-
-
}; 

A union is declared in the same format as a struct or a class. For example,

union Number
{
int x;
double y;
};

indicates that Number is a union type with member int x and double y. The union definition must precede all functions in which it will be used.

1.Program for Union in C++

#include<iostream>
using namespace std;
union WordByte
{
 private:
 unsigned short w;
 unsigned char b[2];
 public:
 unsigned short& word()   
{
return w;
}
unsigned char& lowByte()  
{
return b[0];
}
unsigned char& highByte()  
{
return b[1];
}
} ;
int main()
{
WordByte wb;
wb.word()=256;
count<<"\nWord:"<<(int)wb.word();
count<<"\nLow-byte:"<<(int)wb.lowByte()
count<<"\nHigh-byte:"<<(nt)wb.highByte()
<<endl;
return 0;
}

OUTPUT:

Word:256
Low-Byte:0
High-Byte:1

As with a struct or a class declaration, a union declaration simply creates a new type. Placing a union or struct declaration outside any function does not create a global variable

The only valid built-in operations that can be performed on a union are assigning a union to another union of the same type, taking the address (&) of a union and accessing union members using the structure member operator(.) and the structure pointer operator(->). unions cannot be compared.

Limitation of Union in C++

  • A union cannot be used as a base class in the inheritance(i.e. classes cannot be derived from the union)
  • Unions can have objects as members-only if these objects do not have a constructor, a destructor or an overloaded assignment operator.
  • None of a union’s data members can be declared static

2.Program for Union in c++

#include<iostream>
using std;:cout;
using std::endl;
union Number
{
int integer1;
double double1;
};
int main()
{
Number value;
value.integer1=100;
cout<<"Put a value in the integer member\n"
    <<"and print both member\nint:"
    <<value.integer1<<"\ndouble:"<<value.double1
    <<endl;
cout<<"Put a value in the floating member\n"
    <<"and print both members.\nint:"
    <<value.integer1<<"\ndouble:"<<value.double1
    <<endl;
return 0;
}

OUTPUT:

Put a value in the integer member
and print both members.
int :100
double:-9.25596+061
Put a value in the floating member
and print both members.
int :0
double:100

3.Program for Union in C++

#include<iostream>
using std::cout;
using std::endl;
int main()
{
union
{
int integer1;
double double1;
char *charPtr;
};
int integer2=1;
double double2=3.3;
char *char2Ptr="Anonymous";
cout<<integer2<<' ';
integer1=2;
cout<<integer1<<endl;
cout<<double2<<' ';
double1=4.4;
cout<<double1<<endl;
cout<<char2Ptr<<' ';
charPtr="union";
cout<<charPtr<<endl;
return 0;
}

OUTPUT:

1 2
3.3 4.4
Anonymous union

What is the Difference between Structure and Union in C++

Difference between Union and Structure
Difference between Union and Structure
Translate »