Skip to content

5. 열거체 & 구조체


1. 열거체

#include <iostream>
using namespace std;

enum Status {
    normal = 0,
    abnormal,
    disconnect = 100,
    close
};

void CheckStatus(Status);

int main() {
    Status number_1 = abnormal;
    Status number_2 = Status(100);

    CheckStatus(number_1);
    CheckStatus(number_2);

    return 0;
}

void CheckStatus(Status number) {
    if (number == Status::normal)
        cout << "Status: normal" << endl;
    else if (number == Status::abnormal)
        cout << "Status: abnormal" << endl;
    else if (number == 100)
        cout << "Status: disconnect" << endl;
    else
        cout << "Status: close" << endl;
}
Status: abnormal
Status: disconnect


2. 열거체 클래스

#include <iostream>
using namespace std;

enum class MachineStatus : char {
    normal = 'n',
    abnormal,
    disconnect = 100,
    close
};

void CheckMachineStatus(MachineStatus);

int main() {
    MachineStatus machine = MachineStatus::abnormal;

    CheckMachineStatus(machine);

    return 0;
}

void CheckMachineStatus(MachineStatus machine) {
    if (machine == MachineStatus::normal)
        cout << "Status: normal" << endl;
    else if (machine == MachineStatus::abnormal)
        cout << "Status: abnormal" << endl;
    else if (machine == MachineStatus::disconnect)
        cout << "Status: disconnect" << endl;
    else
        cout << "Status: close" << endl;

    cout << "Machine: " << static_cast<int>(machine)
         << ", " << static_cast<char>(machine) << endl;
}
Status: abnormal
Machine: 111, o


3. 구조체

#include <iostream>
#include <string>
using namespace std;

struct Princess {
    string name;
    string father;
    string birthday = "알 수 없음";
} Goryeo[2];

int main() {
    Princess jungmyung;
    jungmyung.name = "정명공주";
    jungmyung.father = "조선 선조";
    jungmyung.birthday = "1603년 6월 27일";

    Goryeo[0].name = "선정왕후";
    Goryeo[0].father = "고려 성종";
    Goryeo[1].name = "효정공주";
    Goryeo[1].father = "고려 현종";

    cout << "== 조선 공주 ==" << endl;
    cout << jungmyung.name << endl;
    cout << jungmyung.father << endl;
    cout << jungmyung.birthday << endl;

    cout << "== 고려 공주 ==" << endl;
    cout << Goryeo[0].name << endl;
    cout << Goryeo[0].father << endl;
    cout << Goryeo[0].birthday << endl;
    cout << Goryeo[1].name << endl;
    cout << Goryeo[1].father << endl;
    cout << Goryeo[1].birthday << endl;

    return 0;
}
== 조선 공주 ==
정명공주
조선 선조
1603년 6월 27일
== 고려 공주 ==
선정왕후
고려 성종
알 수 없음
효정공주
고려 현종
알 수 없음


4. 구조체 함수 인자

#include <iostream>
#include <string>
using namespace std;

struct Princess {
    string name;
    string father;
    string birthday;
} jungso;

void Print(Princess *);

int main() {
    jungso.name = "정소공주";
    jungso.father = "조선 태종";
    jungso.birthday = "1412년";

    Print(&jungso);

    return 0;
}

void Print(Princess *who) {
    cout << "jungso.name = " << who->name << endl;
    cout << "jungso.father = " << who->father << endl;
    cout << "jungso.birthday = " << who->birthday << endl;
}
jungso.name = 정소공주
jungso.father = 조선 태종
jungso.birthday = 1412년


5. 구조체 초기화 1

#include <iostream>
using namespace std;

struct Data1 {
    Data1(int x) : number(x) {}

    int number;
};

struct Data2 {
    Data2() {
        number = 10;
    }

    int number;
} data2;

int main() {
    Data1 data1 = Data1(2);
    cout << "Data1 number: " << data1.number << endl;
    cout << "Data2 number: " << data2.number << endl;

    return 0;
}
Data1 number: 2
Data2 number: 10


6. 구조체 초기화 2

#include <iostream>
using namespace std;

struct Data1 {
    int number;
};

struct Data2 {
    Data1 data1;

    int number;
} data2;

int main() {
    Data1 data1 = {};
    Data2 data2 = {data1, 10};

    cout << "Data1 number: " << data2.data1.number << endl;
    cout << "Data2 number: " << data2.number << endl;

    return 0;
}
Data1 number: 0
Data2 number: 10


7. 구조체 초기화 3

#include <iostream>
#include <string>
using namespace std;

struct Info {
    string country;

    struct who {
        string name;
        string nickname;
    } who;
};

int main() {
    Info chuck = {"고려", {"장군 척준경", "소드마스터"}};
    Info anjang = {"고구려", {"안장왕 고흥안", "한주 사랑"}};

    cout << chuck.country << ", "
         << chuck.who.name << ", " << chuck.who.nickname << endl;
    cout << anjang.country << ", "
         << anjang.who.name << ", " << anjang.who.nickname << endl;

    return 0;
}
고려, 장군 척준경, 소드마스터
고구려, 안장왕 고흥안, 한주 사랑