首页 > 编程语言 > 详细

【ThinkingInC++】36、联合体

时间:2014-09-06 16:11:43      阅读:265      评论:0      收藏:0      [点我收藏+]
/**
* 书本:【ThinkingInC++】
* 功能:联合
* 时间:2014年9月6日14:51:40
* 作者:cutter_point
*/

#include<iostream>

using namespace std;

union U
{
private:
    int i;
    float f;
public:
    U(int a);
    U(float b);
    ~U();
    int read_int();
    float read_float();
};

U::U(int a)
{
    i=a;
}

U::U(float b)
{
    f=b;
}

U::~U()
{
    cout<<"U::~U()\n";
}

int U::read_int()
{
    return i;
}

float U::read_float()
{
    return f;
}

int main()
{
    U X(12), Y(1.9f);
    cout<<X.read_int()<<endl;
    cout<<X.read_float()<<endl;
    cout<<Y.read_int()<<endl;
    cout<<Y.read_float()<<endl;

    return 0;
}



【ThinkingInC++】36、联合体

原文:http://blog.csdn.net/cutter_point/article/details/39100909

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!