뇌를 자극하는 c++프로그래밍/13

Upload: kanggun0225

Post on 30-May-2018

356 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 C++ /13

    1/14

  • 8/14/2019 C++ /13

    2/14

    (Unions)

    .

    [ 13-3]

    union ManyMembers

    {

    char c;

    short s;

    int i;

    float f;

    double d;

    };

  • 8/14/2019 C++ /13

    3/14

    [ 13-2]

    union MyUnion{

    int i;

    void* p;

    };

    MyUnion uni;

    cout

  • 8/14/2019 C++ /13

    4/14

    (Enumerations)

    enum JOB_KINDS { JOB_DWARF, JOB_WARRIOR, JOB_SORCERER };

    struct Character

    {

    JOB_KINDS jobType;

    // .

    };

    Character c;

    // c .

    // c if (JOB_SORCERER== c.jobType)

    {

    // .

    }

  • 8/14/2019 C++ /13

    5/14

    0 .

    , .

    enum { JOB_DWARD, JOB_WARRIOR, JOB_SORCERER };

    enum { JOB_DWARD = 0, JOB_WARRIOR = 1, JOB_SORCERER = 2};

    =

    enum { JOB_DWARD, JOB_WARRIOR = 3, JOB_SORCERER };

    enum { JOB_DWARD, JOB_WARRIOR = 3, JOB_SORCERER = 4};

    =

  • 8/14/2019 C++ /13

    6/14

    .

    .

    .

    .

    enum Color { RED, BLUE, GREEN, SKYBLUE, MAGENTA, YELLOW };

    Color color1 = RED;

    color1 = SKYBLUE + YELLOW; // Error

    Color1 += 3; // Error

    Color color2;color2 = 5; // Error

    int n = MAGENTA; // n 4 .int m = BLUE + 2; //m 3 .

    Color color3 = (Color)2 // color3 GREEN .Color color4 = (Color)5000 // .

  • 8/14/2019 C++ /13

    7/14

    (References)

    int target = 20;

    // .

    int& ref = target;

    cout

  • 8/14/2019 C++ /13

    8/14

    .

    // .

    float a = 100.0f;

    float b = 12.34f;

    // r a . ( )float& r = a;

    // r 200.0f . ( )r = 200.0f;

    // r b 12.34f . ( )r = b;

    // r 56.7f . ( )r = 56.7f

  • 8/14/2019 C++ /13

    9/14

    Const

    Const .

    Const .

    const int& rci = 100; // OKint& ci = 100; // Error

    char c = A;

    const int& rci = c; // OK

    int& ci = c; // Error

    [ 13-7]

    [ 13-8]

  • 8/14/2019 C++ /13

    10/14

    typedef

    typedef

    typedef unsigned char* uc_ptr;

    unsigned char uc = A;

    uc_ptr p = &uc;

    [ 13-9]

  • 8/14/2019 C++ /13

    11/14

    (Bit Fields)

    struct Flags

    {

    int a : 3;

    int b : 4;

    int : 5; // 5 .bool c : 1;

    };

    [ 13-11]

  • 8/14/2019 C++ /13

    12/14

    struct A

    {

    int i;

    float f;

    };

    struct B

    {

    char c;Aa;

    };

    [ 13-14]

  • 8/14/2019 C++ /13

    13/14

    (= )

    , .

    int arr[10][5];

    // 10 .for ( int i = 0; i < 10; ++i )

    {

    for ( int j = 0; j < 5; ++j )

    arr[i][j] = 10;

    }

    [ 13-15][ 13-16]

  • 8/14/2019 C++ /13

    14/14

    char c = 1;

    char* pc = &c;

    char** ppc = &pc;

    if ( *ppc == pc )

    {

    // .}

    if ( **ppc == c )

    {

    // .

    }

    [ 13-17]