home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C07 / UnionClass.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  699 b   |  37 lines

  1. //: C07:UnionClass.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Unions with constructors and member functions
  7. #include<iostream>
  8. using namespace std;
  9.  
  10. union U {
  11. private: // Access control too!
  12.   int i;
  13.   float f;
  14. public:  
  15.   U(int a);
  16.   U(float b);
  17.   ~U();
  18.   int read_int();
  19.   float read_float();
  20. };
  21.  
  22. U::U(int a) { i = a; }
  23.  
  24. U::U(float b) { f = b;}
  25.  
  26. U::~U() { cout << "U::~U()\n"; }
  27.  
  28. int U::read_int() { return i; }
  29.  
  30. float U::read_float() { return f; }
  31.  
  32. int main() {
  33.   U X(12), Y(1.9F);
  34.   cout << X.read_int() << endl;
  35.   cout << Y.read_float() << endl;
  36. } ///:~
  37.