typedef - enum - struct

typedef

為一個已有的類型取一個新的名字

  • 基礎數(shù)據(jù)類型
  • 結構體類型
#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; // 從自定義的數(shù)字開始排列
    return 0;
}
0
3

struct 結構體

struct 與 class 初始化

1.若類和結構體所有數(shù)據(jù)成員均為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() {
    // 若類和結構體所有數(shù)據(jù)成員均為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.若數(shù)據(jù)成員有private或protected型,或是提供了構造函數(shù),必須使用構造函數(shù)來進行初始化。

#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"); // 構造函數(shù),沒有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; // 從數(shù)組中獲得相應下標的元素
    
    pStudent pStu = new Student(); // 開辟一個存放Student的空間
    pStu->name = "Eric";
    cout << "姓名:" << pStu->name << endl;

    return 0;
}

20
年齡:20
姓名:shuai
性別:0
性別:male
性別:male

姓名:Eric
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容