typedef - enum - struct

typedef

为一个已有的类型取一个新的名字

  • 基础数据类型
  • 结构体类型
#include <iostream>

using namespace std;

typedef int Age;

int main() {
    Age age = 20; // typedef int Age;
    cout << age << endl;
    return 0;
}
20

enum

枚举类型

#include <iostream>

using namespace std;

int main() {
    enum days {
        one, two = 2, three
    } day;
    day = one;
    cout << day << endl;
    cout << three << endl; // 从自定义的数字开始排列
    return 0;
}
0
3

struct 结构体

struct 与 class 初始化

1.若类和结构体所有数据成员均为public型,可采取如下带花括号形式进行初始化。

#include <iostream>

using namespace std;

// struct的成员默认是public型
struct sStudent {
    int age;
    string name;
};

// class的成员默认是private型
class cStudent {
public:
    int age;
    string name;
};

int main() {
    // 若类和结构体所有数据成员均为public型,可采取如下带花括号形式进行初始化
    sStudent a = {20, "Adele"}; // 结构体可以这样初始化
    cout << a.age << '\t' << a.name << endl;

    cStudent b = {20, "Bob"};
    cout << b.age << '\t' << b.name << endl;
    return 0;
}
20  Adele
20  Bob

2.若数据成员有private或protected型,或是提供了构造函数,必须使用构造函数来进行初始化。

#include <iostream>

using namespace std;

// struct的成员默认是public型
struct sStudent {
private:
    int age;
    string name;
public:
    string school;

    sStudent(int a, string n, string sch) {
        age = a;
        name = n;
        school = sch;
    }

    void show() {
        cout << age << '\t' << name << '\t' << school << endl;
    }
};

// class的成员默认是private型
class cStudent {
    int age;
    string name;
    string school;
public:
    cStudent(int a, string n, string sch) {
        age = a;
        name = n;
        school = sch;
    }

    void show() {
        cout << age << '\t' << name << '\t' << school << endl;
    }
};

int main() {
    sStudent a = sStudent(20, "Adele", "CSU"); // 构造函数,没有new
    cStudent b = cStudent(20, "Bob", "CSU");
    cout << a.school << endl; // school成员是public型
    a.show();
    b.show();
    return 0;
}
CSU
20  Adele   CSU
20  Bob CSU

typedef, enum, struct 综合实例

#include <iostream>

using namespace std;

enum Sex {
    male, female, other
};

string getSex(int s) {
    switch (s) {
        case male:
            return "male";
        case female:
            return "female";
        case other:
            return "other";
        default:
            return "wrong sex";
    }
}

char sex[3][7] = {"male", "female", "other"}; // 这种方式好些

typedef int Age;

typedef struct {
    string name;
    int age;
    Sex sex;
} Student, *pStudent; // 有了typedef,在定义的时候可以不用些struct



int main() {
    Age age = 20; // typedef int Age;
    cout << age << endl;

    Student stu;
    stu.age = 20;
    stu.name = "shuai";
    stu.sex = male;

    cout << "年龄:" << stu.age << endl;
    cout << "姓名:" << stu.name << endl;
    cout << "性别:" << stu.sex << endl;
    cout << "性别:" << getSex(stu.sex) << endl;
    cout << "性别:" << sex[stu.sex] << endl << endl; // 从数组中获得相应下标的元素
    
    pStudent pStu = new Student(); // 开辟一个存放Student的空间
    pStu->name = "Eric";
    cout << "姓名:" << pStu->name << endl;

    return 0;
}

20
年龄:20
姓名:shuai
性别:0
性别:male
性别:male

姓名:Eric
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容