Unions and Class Assignment Help

Assignment Help: >> Union, Nested Classes, Constructors and Destructors - Unions and Class

Unions and class

Union is a user defined data type which size is enough to hold one of its members. At most, one of the members could be stored in a union at any time. Union is also used for declaring classes in C++ programming.   The members of a union are public by default.

A union permits storing its members only one at a time. A union might have member functions involving constructors and destructors, but not virtual functions. A union might not have base class. A class objects along with a constructor or a destructor or a user defined assignment operator cannot be a member of a union.  A union cannot have static data member.

The common form is

union user_defined_name

{

private:

//data;

//methods;

public:

//methods;

};

user_defined_name object;

Example:

union sample

{

public:

int a;

char name;

void display ( );

void sum ( );

};

// sample program

# include<iostream.h>

union sample

{

private:

int x;

float y;

public:

void getinfo ( );

void disinfo ( );

};

void sample :: getinfo ( )

{

cout<<"value of x (in integer):";

cin>> x;

cout<<"value of y (in float):";

cin>> y;

}

void sample:: disinfo( )

{

cout<< endl;

cout<<"x = "<<x<< endl;

cout << "y= " << y << endl;

}

void main( void)

{

sample obj;

cout<<"enter the information"<< endl;

obj.getinfo( );

cout<<"\ content of union"<< endl;

obj.disinfo( );

}

the output is

enter the information

value of x (in integer ) : 45

value of y( in float ) :  9.89

content of union

x = 45

y = 9.89

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd