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




u1401_1.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
 
int main() {
#if -1 > 0
   std::cout << "mm"
             << std::endl;
#elif 1 > 0
   std::cout << "ok"
             << std::endl;
#else
   std::cout << "xx"
             << std::endl;
#endif
 
   return 0;
}
  
/* Kaiching Chang
   u1401_1.cpp
   2014-02 */

u1401_2.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#define DEMO1 10
 
int main() {
#ifdef DEMO1
   std::cout << "demo1"
             << std::endl;
#else
   std::cout << "no demo1"
             << std::endl;
#endif
 
#ifndef DEMO2
   std::cout << "no demo2"
             << std::endl;
#else
   std::cout << "demo2"
             << std::endl;
#endif
 
   return 0;
}
  
/* Kaiching Chang
   u1401_2.cpp
   2014-02 */

u1402_1.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#define DEMO 10
 
int main() {
   std::cout << DEMO
             << std::endl;
 
#undef DEMO
 
   //std::cout << DEMO
   //          << std::endl;
 
   return 0;
}
  
/* Kaiching Chang
   u1402_1.cpp
   2014-02 */

u1402_2.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#define Prints(arg) std::cout << #arg
 
int main() {
   Prints(hello);
   std::cout << std::endl;
 
   return 0;
}
  
/* Kaiching Chang
   u1402_2.cpp
   2014-02 */

u1402_3.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#define F(n, a) int f##n() {return a;}
 
F(test, 0);
F(function, 1);
F(rrr, 2);
 
int main() {
   std::cout << ftest()
             << std::endl;
   std::cout << ffunction()
             << std::endl;
   std::cout << frrr()
             << std::endl;
 
   return 0;
}
  
/* Kaiching Chang
   u1402_3.cpp
   2014-02 */

u1403_1.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
 
int main() {
   std::string s = "There is no spoon.";
   std::cout << s.find("no")
             << std::endl;
 
   return 0;
}
  
/* Kaiching Chang
   u1403_1.cpp
   2014-02 */

u1403_2.h


1
2
3
4
5
6
7
8
9
10
11
class Demo {
public:
   int a;
   int b;
    
   int do_something();
};
  
/* Kaiching Chang
   u1403_2.h
   2014-02 */

u1403_2.cpp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "u1403_2.h"
 
int Demo::do_something() {
   return a + b;
}
 
int main() {
   Demo d;
   d.a = 1;
   d.b = 2;
   std::cout << d.do_something()
             << std::endl;
 
   return 0;
}
  
/* Kaiching Chang
   u1403_2.cpp
   2014-02 */

the end

沒有留言: