木之夏  
海纳百川,有容乃大;壁立千仞,无欲则刚

typedef - 自定义类型

该语句主要用于将某种类型起名为一个别名,如 double 起名为 area,这样以后再定义area类型的变量时,就相当于定义了一个double的变量。

 

程序实例:

定义一个double 类的

#include <iostream>
using namespace std;

int main()
{

    typedef double area, volume;
    area A1 = 3.23;
    volume V1 = 100.32;
    cout << "the size of area= " << sizeof A1 <<"bytes"<<"\n" ;
    cout << "the size of volume= " << sizeof volume << "bytes"<< "\n";
    return 0;
}

the size of area= 8bytes
the size of volume= 8bytes

也可以使用C 语言继承过来的using

语法说明:

using(新类型名称=已有类型明)

#include <iostream>
using namespace std;

int main()
{

    using area=double;
    using  volume = double;
    area A1 = 3.23;
    volume V1 = 100.32;
    cout << "the size of area= " << sizeof A1 <<"bytes"<<"\n" ;
    cout << "the size of volume= " << sizeof volume << "bytes"<< "\n";
    return 0;
}

运行结果:

the size of area= 8bytes
the size of volume= 8bytes

 

posted on 2021-02-13 11:21  木之夏  阅读(224)  评论(0)    收藏  举报