测试基本数据类型以及复合类型的长度

//测试各种数据类型的长度,以及string类的长度
#include <iostream>
#include <string>
using namespace std;

struct stru0
{
    string name;
    int num;
    float score;
};

enum enu0
{
    one,
    two,
    three
};

class cla0
{
};

class cla1
{
    public:
        cla1(int ntemp0,int ntemp1)
        {
            nnum0 = ntemp0;
            nnum1 = ntemp0;
        }
        int nnum0;
        int nnum1;
};

int main()

    /*
     * 测试内置数据类型的长度
     */
    cout<<"type and length"<<endl;
    cout<<"char: "
        <<sizeof(char)<<endl;
    cout<<"short int: "
        <<sizeof(short int)<<endl;
    cout<<"int: "
        <<sizeof(int)<<endl;
    cout<<"long int: "
        <<sizeof(long int)<<endl;
    cout<<"long long int: "
        <<sizeof(long long int)<<endl;
    cout<<"float: "
        <<sizeof(float)<<endl;
    cout<<"double: "
        <<sizeof(double)<<endl;
    cout<<"long double: "
        <<sizeof(long double)<<endl
        <<endl;
   
    /*
     * 测试一个复合类型之枚举类型
     */   
    cout<<"enum: enu0 with 'one' 'two' 'three'"<<endl;
    enu0 enu0Type0;
    cout<<"enu0 type: "
        <<sizeof(enu0)<<endl;
    cout<<"enu0 type: "
        <<sizeof(enu0Type0)<<endl
        <<endl;
   
    /*
     * 测试一个复合类型之结构体类型
     */ 
    cout<<"struct: stru0 with a 'string' and  a 'int' and  a 'float'"<<endl;  
    stru0 stru0Type0={"addfqwde",12,123.1};
    cout<<"stru0 type: "
        <<sizeof(stru0)<<endl;
    cout<<"stru0 var: "
        <<sizeof(stru0Type0)<<endl
        <<endl;
   
    /*
     * 测试一个复合类型之类类型
     */     
    cout<<"class: cla0 with no contain"<<endl;
    cla0 object0;
    cout<<"cla0 type: "
        <<sizeof(cla0)<<endl;
    cout<<"cla0 object: "
        <<sizeof(object0)<<endl
        <<endl;
   
    cout<<"class: cla1 with two 'int'"<<endl;
    cla1 object1(12,23);
    cout<<"cla1 type: "
        <<sizeof(cla1)<<endl;
    cout<<"cla1 object: "
        <<sizeof(object1)<<endl;
}

转载于:https://my.oschina.net/libowen/blog/93461

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值