* Read the section Defining Unions on pp. 417–418 and answer the following questions in 200 to 300 words. Use your own words and do not copy directly from the text.
* Define a union; then, explain how a union may be used.
* Explain how sharing may be associated with unions.
* What are the alternatives to unions?
According to the text and other resources on the internet a union in C++ is defined as a class or data type at any given time that only contains one object, although that object can be an array. Unions can be used to store a large amount of data and may contain access specifiers including destructors and constructors but may not contain static data members or virtual functions. Unions may be used in two ways for what I understand, one way is for a single type of data to be accessed in several ways and another is a polymorphic type.
When it comes to sharing and unions they can go hand and hand, for example if you only have a limited amount of memory, storage or just want to save some you can do this by using a single memory location or a union. Doing this allows the user to use a single memory location to store different data types in the same location. Using a union also allows for more possible data types to be stored just in case the used doesn’t know what type of data will be passed to the function.
After reading the text and some good information web sites I came to the conclusion that there are alternatives to unions, one being a class and the other being a struct. These alternatives give the programmer the option so they don’t just have to use a union or vice versa. During my reading I came across many comments as to why no to use unions and instead use the other alternatives. Now I have not had the opportunity to use unions for myself so these statements can only be taken as other peoples opinions until I have one of my own.