Skip to content

Commit 75b6a47

Browse files
author
Changkun Ou
committed
commit all code
1 parent 9fd7314 commit 75b6a47

33 files changed

+1294
-2
lines changed

1/1.1.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// 1.1.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
8+
#include "foo.h"
9+
10+
int main() {
11+
add(1, 2);
12+
return 0;
13+
}

1/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TARGET = 1.1
2+
all:
3+
gcc -c foo.c
4+
g++ 1.1.cpp foo.o -o $(TARGET)
5+
clean:
6+
rm -rf *.o $(TARGET)

1/foo.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// foo.c
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
8+
9+
#include "foo.h"
10+
11+
int add(int x, int y) {
12+
return x+y;
13+
}

1/foo.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// foo.h
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
8+
9+
#ifdef __cplusplus
10+
extern "C" {
11+
#endif
12+
13+
int add(int x, int y);
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif

2/2.1.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// 2.1.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
// nullptr
8+
9+
#include <iostream>
10+
11+
void foo(char *);
12+
void foo(int);
13+
14+
int main() {
15+
16+
if(NULL == (void *)0)
17+
std::cout << "NULL == 0" << std::endl; // 该行将输出
18+
else
19+
std::cout << "NULL != 0" << std::endl;
20+
21+
foo(0); // 调用 foo(int)
22+
//foo(NULL); // 该行不能通过编译
23+
foo(nullptr); // 调用 foo(char*)
24+
25+
return 0;
26+
}
27+
void foo(char *ch) {
28+
std::cout << "call foo(char*)" << std::endl;
29+
}
30+
void foo(int i) {
31+
std::cout << "call foo(int)" << std::endl;
32+
}

2/2.2.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// 2.2.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
// constexpr
8+
9+
#include <iostream>
10+
#define LEN 10
11+
12+
constexpr int len_foo() {
13+
return 5;
14+
}
15+
16+
constexpr int fibonacci(const int n) {
17+
return n == 1 || n == 2 ? 1 : fibonacci(n-1)+fibonacci(n-2);
18+
}
19+
20+
int main() {
21+
char arr_1[10];
22+
23+
char arr_2[LEN];
24+
25+
const int len = 10;
26+
char arr_3[len];
27+
28+
char arr_5[len_foo()+5];
29+
30+
std::cout << fibonacci(10) << std::endl;
31+
// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
32+
33+
return 0;
34+
}

2/2.3.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// 2.3.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
// auto/decltype/尾返回类型/返回类型推导
8+
9+
10+
#include <iostream>
11+
12+
// 传统 C++
13+
template <typename R, typename T, typename U>
14+
R add1(T x, U y) {
15+
return x+y;
16+
}
17+
18+
// 尾返回类型
19+
template <typename T, typename U>
20+
auto add2(T x, U y) -> decltype(x+y) {
21+
return x+y;
22+
}
23+
24+
// C++14 返回类型推导
25+
template <typename T, typename U>
26+
auto add3(T x, U y) {
27+
return x+y;
28+
}
29+
30+
int main() {
31+
auto i = 5;
32+
33+
int arr[10] = {0};
34+
auto auto_arr = arr; // 正确,对整个类型进行推导
35+
//auto auto_arr2[10] = arr; // 错误, 无法推导数组元素类型
36+
37+
auto x = 1;
38+
auto y = 2;
39+
decltype(x+y) z1;
40+
//auto z2; // 错误, 无法推导
41+
42+
43+
std::cout << add1<int, int, int>(1,1) << std::endl;
44+
std::cout << add1<int, int>(1,1) << std::endl;
45+
std::cout << add1<int, int>(1,1) << std::endl;
46+
47+
48+
return 0;
49+
}

2/2.4.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 2.4.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
// 区间迭代
8+
9+
#include <iostream>
10+
#include <vector>
11+
12+
int main() {
13+
int array[] = {1,2,3,4,5};
14+
for(auto &x : array) {
15+
std::cout << x << std::endl;
16+
}
17+
18+
// 传统 C++ 写法
19+
std::vector<int> arr(5, 100);
20+
for(std::vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) {
21+
std::cout << *i << std::endl;
22+
}
23+
24+
// C++11 写法
25+
// & 启用了引用, 如果没有则对 arr 中的元素只能读取不能修改
26+
for(auto &i : arr) {
27+
std::cout << i << std::endl;
28+
}
29+
return 0;
30+
}

2/2.5.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// 2.5.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
// 初始化列表
8+
9+
10+
#include <initializer_list>
11+
12+
class Foo {
13+
private:
14+
int value;
15+
public:
16+
Foo(int) {}
17+
};
18+
19+
class Magic {
20+
public:
21+
Magic(std::initializer_list<int> list) {}
22+
};
23+
24+
void func(std::initializer_list<int> list) {
25+
return;
26+
}
27+
28+
int main() {
29+
int arr[3] = {1,2,3}; // 列表初始化
30+
Foo foo(1); // 普通构造初始化
31+
32+
Magic magic = {1,2,3,4,5}; // 使用 initialize_list
33+
func({1,2,3});
34+
}

2/2.6.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//
2+
// 2.6.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
// 模板增强
8+
9+
10+
#include <iostream>
11+
#include <vector>
12+
#include <string>
13+
14+
template class std::vector<bool>; // 强行实例化
15+
extern template class std::vector<double>; // 不在该编译文件中实例化模板
16+
17+
18+
template< typename T, typename U, int value>
19+
class SuckType {
20+
public:
21+
T a;
22+
U b;
23+
SuckType():a(value),b(value){}
24+
};
25+
// template< typename T>
26+
// typedef SuckType<std::vector<int>, T, 1> NewType; // 不合法
27+
28+
template <typename T>
29+
using NewType = SuckType<int, T, 1>; // 合法
30+
31+
// 默认模板类型
32+
template<typename T = int, typename U = int>
33+
auto add(T x, U y) -> decltype(x+y) {
34+
return x+y;
35+
}
36+
37+
// sizeof...
38+
template<typename... Args>
39+
void magic(Args... args) {
40+
std::cout << sizeof...(args) << std::endl;
41+
}
42+
43+
44+
// 1. 递归解参数包
45+
template<typename T>
46+
void printf1(T value) {
47+
std::cout << value << std::endl;
48+
}
49+
template<typename T, typename... Args>
50+
void printf1(T value, Args... args) {
51+
std::cout << value << std::endl;
52+
printf1(args...);
53+
}
54+
// 2.初始化列表展开解参数包
55+
template<typename T, typename... Args>
56+
auto printf2(T value, Args... args) {
57+
std::cout << value << std::endl;
58+
return std::initializer_list<T>{([&] {
59+
std::cout << args << std::endl;
60+
}(), value)...};
61+
}
62+
63+
int main() {
64+
65+
std::vector<std::vector<int>> wow; // 注意尖括号
66+
67+
NewType<int> t;
68+
69+
magic();
70+
magic(1);
71+
magic(1,"");
72+
73+
printf1(1, 2.3, "abc");
74+
printf2(1, 2.3, "abc");
75+
return 0;
76+
}

2/2.7.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// 2.7.cpp
3+
// c++1x tutorial
4+
//
5+
// created by changkun at shiyanlou.com
6+
//
7+
// 面向对象增强
8+
9+
#include <iostream>
10+
#include <string>
11+
class Base {
12+
public:
13+
std::string str;
14+
int value;
15+
Base() = delete;
16+
Base(std::string s) {
17+
str = s;
18+
}
19+
20+
// 委托构造
21+
Base(std::string s, int v) : Base(s) {
22+
value = v;
23+
}
24+
25+
// 终止重载
26+
virtual void foo() final {
27+
return;
28+
}
29+
virtual void foo(int v) {
30+
value = v;
31+
}
32+
};
33+
class Subclass final : public Base {
34+
public:
35+
double floating;
36+
Subclass() = delete;
37+
38+
// 继承构造
39+
Subclass(double f, int v, std::string s) : Base(s, v) {
40+
floating = f;
41+
}
42+
43+
// 显式重载
44+
virtual void foo(int v) override {
45+
std::cout << v << std::endl;
46+
value = v;
47+
}
48+
}; // 合法 final
49+
50+
// class Subclass2 : Subclass {
51+
// }; // 非法, Subclass 已 final
52+
// class Subclass3 : Base {
53+
// void foo(); // 非法, foo 已 final
54+
// }
55+
56+
int main() {
57+
// Subclass oops; // 非法, 默认构造已删除
58+
Subclass s(1.2, 3, "abc");
59+
60+
s.foo(1);
61+
62+
std::cout << s.floating << std::endl;
63+
std::cout << s.value << std::endl;
64+
std::cout << s.str << std::endl;
65+
}

0 commit comments

Comments
 (0)