C++ examples for Data Type:union
An example of a union.
#include <iostream> using namespace std; union Number //from w w w. java 2 s . c o m { int integer1; double double1; }; int main() { Number value; // union variable value.integer1 = 100; // assign 100 to member integer1 cout << "Put a value in the integer member\n" << "and print both members.\nint: " << value.integer1 << "\ndouble: " << value.double1 << endl; value.double1 = 100.0; // assign 100.0 to member double1 cout << "Put a value in the floating member\n" << "and print both members.\nint: " << value.integer1 << "\ndouble: " << value.double1 << endl; }