Union
#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;
/* Same as structure but all the members of the union uses the same location whereas in structure each member will have its own location. */
union Student
{
int roll_no; /* Declaring members of union. */
int mark;
}s; /* Declaring an union variable. */
int main()
{
s.roll_no=20;
cout<< "Roll number of Student:" <<s.roll_no <<endl; // we should use one member at a time since all members share the same location.
s.mark=95;
cout<< "Mark of Student:" <<s.mark <<endl;
return 0;
}
Output:
Roll number of Student:20
Mark of Student:95