C++ 速查手冊 V1.00 - 單元 7 範例




u0701_1.cpp


#include <iostream>

int main() {
   int a = 22;
   int& a_ref = a;

   std::cout << "a: "
             << a
             << std::endl;
   std::cout << "a_ref: "
             << a_ref
             << std::endl;

   a_ref = 11;
   std::cout << "a: "
             << a
             << std::endl;
   std::cout << "a_ref: "
             << a_ref
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0701_1.cpp
   2014-02 */

u0701_2.cpp


#include <iostream>

int main() {
   int a = 22;
   int b = 33;
   int c = 11;
   int&& ref = a + b + c;

   std::cout << "a: "
             << a
             << std::endl;
   std::cout << "b: "
             << b
             << std::endl;
   std::cout << "c: "
             << c
             << std::endl;
   std::cout << "ref: "
             << ref
             << std::endl;

   ref += 66;
   std::cout << "a: "
             << a
             << std::endl;
   std::cout << "b: "
             << b
             << std::endl;
   std::cout << "c: "
             << c
             << std::endl;
   std::cout << "ref: "
             << ref
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0701_2.cpp
   2014-02 */

u0702_1.cpp


#include <iostream>
 
int main() {
   enum Day {Sunday, Monday, Tuesday,
             Wednesday, Thursday,
             Friday, Saturday};

   std::cout << "Sunday: "
             << Sunday
             << std::endl;
   std::cout << "Monday: "
             << Monday
             << std::endl;
   std::cout << "Tuesday: "
             << Tuesday
             << std::endl;
   std::cout << "Wednesday: "
             << Wednesday
             << std::endl;
   std::cout << "Thursday: "
             << Thursday
             << std::endl;
   std::cout << "Friday: "
             << Friday
             << std::endl;
   std::cout << "Saturday: "
             << Saturday
             << std::endl;

   Day today = Wednesday;
   std::cout << "Today: "
             << today
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0702_1.cpp
   2014-02 */

u0702_2.cpp


#include <iostream>
 
int main() {
   enum {apple, banana = 11,
         orange, peach = 5};

   std::cout << "apple: "
             << apple
             << std::endl;
   std::cout << "banana: "
             << banana
             << std::endl;
   std::cout << "orange: "
             << orange
             << std::endl;
   std::cout << "peach: "
             << peach
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0702_2.cpp
   2014-02 */

u0702_3.cpp


#include <iostream>

enum class Color: char {
   RED = 'r',
   GREEN = 'g',
   BLUE = 'b',
};
 
int main() {
   Color r;
   r = Color::RED;
   Color g;
   g = Color::GREEN;
   Color b;
   b = Color::BLUE;

   std::cout << "RED: "
             << static_cast<char>(r)
             << std::endl;
   std::cout << "GREEN: "
             << static_cast<char>(g)
             << std::endl;
   std::cout << "BLUE: "
             << static_cast<char>(b)
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0702_3.cpp
   2014-02 */

u0703_1.cpp


#include <iostream>

int main() {
   int a[5];
   a[0] = 1;
   a[1] = 2;
   a[2] = 3;
   a[3] = 4;
   a[4] = 5;

   std::cout << "a[0]: "
             << a[0]
             << std::endl;
   std::cout << "a[1]: "
             << a[1]
             << std::endl;
   std::cout << "a[2]: "
             << a[2]
             << std::endl;
   std::cout << "a[3]: "
             << a[3]
             << std::endl;
   std::cout << "a[4]: "
             << a[4]
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0703_1.cpp
   2014-02 */

u0703_2.cpp


#include <iostream>

int main() {
   int c[2][3] = {{1, 2, 3},
                  {4, 5, 6}};

   for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 3; j++) {
         std::cout << c[i][j] 
         << std::endl;
      }
   }

   return 0;
}

/* Kaiching Chang 
   u0703_2.cpp
   2014-02 */

u0704_1.cpp


#include <iostream>
 
int main() {
   int a = 22;
   int* a_ptr = &a;
   std::cout << "a_ptr: "
             << a_ptr
             << std::endl;
   std::cout << "*a_ptr: "
             << *a_ptr
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0704_1.cpp
   2014-02 */

u0704_2.cpp


#include <iostream>

int main() {
   int a[] = {1, 2, 3, 4, 5};

   std::cout << "a[2]: "
             << *(a + 2)
             << std::endl;
   std::cout << "a[4]: "
             << *(a + 4)
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0704_2.cpp
   2014-02 */

u0704_3.cpp


#include <iostream>

int main() {
   int n = 1;
   int* p = &n;
   void* p2 = p;
   int* p3 = static_cast<int*>(p2);

   std::cout << "n: "
             << n
             << std::endl;
   std::cout << "p: "
             << p
             << std::endl;
   std::cout << "*p3: "
             << *p3
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0704_3.cpp
   2014-02 */

u0704_4.cpp


#include <iostream>

int main() {
   int n = 22;
   std::cout << "n: "
             << n
             << std::endl;

   int* p = &n;
   std::cout << "p: "
             << p
             << std::endl;

   p = nullptr; // NULL
   std::cout << "p: "
             << p
             << std::endl;

   return 0;
}

/* Kaiching Chang 
   u0704_4.cpp
   2014-02 */

u0705_1.cpp


#include <iostream>

struct Demo {
   int member1;
   char *member2;
   float member3;
};

int main() {
   Demo d;
   d.member1 = 19823;
   d.member2 = "203";
   d.member3 = 3.011;

   std::cout << "member1: "
             << d.member1
             << std::endl;
   std::cout << "member2: "
             << d.member2
             << std::endl;
   std::cout << "member3: "
             << d.member3
             << std::endl;

   return 0; 
}

/* Kaiching Chang 
   u0705_1.cpp
   2014-02 */

u0705_2.cpp


#include <iostream>

struct Demo {
   int member1;
   float member2;
   char *member3;
    
   void member4();
};

void Demo::member4() {
   std::cout << "do something"
             << std::endl;
}

int main() {
   Demo d;
   d.member4(); 

   return 0;
}

/* Kaiching Chang 
   u0705_2.cpp
   2014-02 */

u0706.cpp


#include <iostream>

union Demo {
   int member1;
   float member2;
   char *member3;
};

int main() {
   Demo d;
   d.member1 = 1;
   d.member2 = 2.0;
   d.member3 = "3";

   std::cout << "member1: "
             << d.member1
             << std::endl;
   std::cout << "member2: "
             << d.member2
             << std::endl;
   std::cout << "member3: "
             << d.member3
             << std::endl;

    return 0; 
}

/* Kaiching Chang 
   u0706.cpp
   2014-02 */

the end

沒有留言: