Unions and Classes are Related : Union Class « Class « C++






Unions and Classes are Related

  
#include <iostream>  
using namespace std;
union u_type {  
  u_type(int a);  // public by default  
  void showchars(void);  
  int i;  
  char ch[2];  
};  
     
u_type::u_type(int a)  
{  
  i = a;  
}  
     
void u_type::showchars(void)  
{  
  cout << ch[0] << " ";  
  cout << ch[1] << "\n";  
}  
     
main(void)  
{  
  u_type u(1000);  
     
  u.showchars();  
     
  return 0;  
}
  
    
  








Related examples in the same category

1.Use union to define classUse union to define class
2.An anonymous union is a union that has neither a tag name nor any objects specified in its declaration.
3.Anonymous union