forked from thantrieu/C-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBai50.cpp
61 lines (50 loc) · 1.06 KB
/
Bai50.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
Con trỏ this
- Vấn đề:
- Là cái gì?
- Sử dụng khi nào?
*/
#include <iostream>
using namespace std;
template<class T> T findMax(T a, T b) {
return (a > b) ? a : b;
}
template<class T> void showInfo(T* arr, size_t size) {
for (size_t i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << "=======================" << endl;
}
template<class T> class Info {
private:
T* data;
size_t size;
public:
Info() {
}
Info(T* data, size_t size) {
this->data = data;
this->size = size;
}
template<class T> void showData() {
for (size_t i = 0; i < size; i++)
{
cout << data[i] << " ";
}
cout << "\n=========================\n";
}
};
int main() {
cout << findMax<int>(10, 20) << endl;
char arr[] = { 'h', 'e', 'l', 'l', 'o', '!' };
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
float x[] = { 1.23f, 3.45f, 6.2f, 7.9f, 4.6f, 7.8f, 9.5f };
Info<char> f1(arr, 5);
Info<int> f2(a, sizeof(a) / sizeof(int));
Info<float> f3(x, sizeof(x) / sizeof(float));
f1.showData<char>();
f2.showData<int>();
f3.showData<float>();
return 0;
}