Structures

C++ allows you to drop the keyword struct when you declare structure variables:ui

struct inflatable
{
    char name[20];
    float volume;
    double price;
};

struct inflatable goose;    //keyword struct required in C
inflatable vincent;            //keyword struct not required in C++

Union

A union is a data that can hold different data types but only one type at a time.A union can hold an int or a long or a double.The syntax is like that for a structure,but the meaning is different.For example:code

union one4all
{
    int int_val;
    long lone_val;
    double double_val;
};

You can use a one4all variable to hold an int,a long,or a double,just as you do so at different times:io

one4all pail;
pail.int_val = 15;
cout<<pail.int_val;
pail.double_val = 1.38;
cout<<pail.double_val;
相關文章
相關標籤/搜索