Yurttas/PL/OOL/Cplusplus/F/02/05/02/03/union 00.cpp

 1/*
 2   Copyright(C) 1998
 3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 4
 5   Permission to use, copy, modify, and distribute this
 6   software and its documentation for EDUCATIONAL purposes
 7   and without fee is hereby granted provided that this
 8   copyright notice appears in all copies.
 9 
10   date   : January 1, 1998.
11   author : Salih Yurttas.
12
13   union_00.cpp
14*/
15
16
17#include <iostream>
18
19using namespace std;
20
21int main(int argc, char* argv[]) {
22
23  union u { int i;
24            char c;
25            float f;
26          };
27
28  union u u0;
29
30  cout << "-->   int u0.i : ";
31  cin >> u0.i;
32
33  cout << endl;
34  cout << "u0.i = " << u0.i << endl;
35  cout << "u0.c = " << u0.c << endl; // no !
36  cout << "u0.f = " << u0.f << endl; // no !
37  cout << endl;
38
39  cout << "-->  char u0.c : ";
40  cin >> u0.c;
41
42  cout << endl;
43  cout << "u0.i = " << u0.i << endl; // no !
44  cout << "u0.c = " << u0.c << endl;
45  cout << "u0.f = " << u0.f << endl; // no !
46  cout << endl;
47
48  cout << "--> float u0.f : ";
49  cin >> u0.f;
50
51  cout << endl;
52  cout << "u0.i = " << u0.i << endl; // no !
53  cout << "u0.c = " << u0.c << endl; // no !
54  cout << "u0.f = " << u0.f << endl;
55  cout << endl;
56
57}