Revision c28093c1

View differences:

fiddle/Makefile
1
template:
2
	$(CXX) template_function.cpp -o template_function
3
	./template_function
fiddle/pointer.cpp
1
/*
2
TESTING POINTER
3
*/
4

  
5
#include <stdio.h>
6
#include <iostream>
7
using namespace std;
8

  
9
int func_reference(int& r)
10
{
11
    // cout << "*r = " << *r << endl; // ERROR
12
    cout << "&r = " << &r << endl;
13
    cout << "r = " << r << endl;
14
    r = 5;
15
    cout << "r = " << r << endl;
16
}
17

  
18
int main()
19
{
20
    int ** ramon;
21
    int *paul;
22
    int melissa = 5;
23
    paul = &melissa;
24
    ramon = &paul;
25

  
26
    printf("ramon = %d\n", ramon);
27
    printf("&paul = %d\n", &paul);
28
    printf("*ramon = %d\n", *ramon);
29
    printf("&melissa = %d\n", &melissa);
30
    printf("**ramon = %d\n", **ramon);
31

  
32
    int * xuan;
33
    xuan = &melissa;
34
    printf("*xuan = %d\n", *xuan);
35
    printf("&xuan = %d\n", &xuan);
36

  
37
    int* quynh;
38
    quynh = &melissa;
39
    printf("*quynh = %d\n", *quynh);
40
    printf("&quynh = %d\n", &quynh);
41

  
42
    const char * name = "ABCDE";
43
    std::cout << name;
44
    std::cout << "\n";
45

  
46
    // printf("&name = %s\n", &&name);
47
    printf("name = %s\n", name); // ABCDE
48
    printf("&(*name) = %s\n", &(*name)); // ABCDE
49
    printf("*name %%c = %c\n", *name); // A
50
    printf("*name %%u = %u\n", *name); // 65, some number, and it's the number of the address
51
    printf("*name %%d = %d\n", *name); // 65, some number, and it's the number of the address
52
    printf("(*name + 1) %%d = %d\n", (*name + 1)); // 66, some number, and it's the number of the address
53
    printf("(*name + 1) %%c = %c\n", (*name + 1)); // B
54
    printf("name[1] = %c\n", name[1]); // B
55
    cout << endl;
56

  
57
    cout << "******* Pass by reference ********" << endl << endl;
58
    int x = 16;
59

  
60
    cout << "# int arr[] = {7, 11, 13};" << endl;
61
    int arr[] = {7, 11, 13};
62
    cout << "arr = \t\t" << arr << endl;
63
    cout << "&(*arr)) = \t" << &(*arr) << endl;
64
    cout << "&arr = \t\t" << &arr << endl;
65
    cout << "&arr = " << long(&arr) << endl;
66
    cout << "??? why why why, plusing with 4, resulting in plusing with 16 ???" << endl;
67
    cout << "sizeof(int) = " << long(sizeof(int)) << endl;
68
    cout << "arr + sizeof(int) = " << long(arr + sizeof(int)) << endl;
69
    cout << "arr + 2*sizeof(int) = " << long(arr + 2*sizeof(int)) << endl;
70
    cout << "//" << endl;
71
    cout << "&arr + sizeof(int) = " << long(&arr + sizeof(int)) << endl;
72
    cout << "arr + 4 = " << long(arr + 4) << endl;
73
    cout << "arr + 8 = " << long(arr + 8) << endl;
74
    cout << "&arr[1] = " << &arr[1] << endl;
75
    cout << "*(&arr[1]) = " << *&arr[1] << endl;
76
    cout << "arr[1] = " << arr[1] << endl;
77
    cout << endl;
78

  
79
    cout << "# char char_arr[] = {'x', 'u', 'a', 'n', '\0'};" << endl;
80
    char char_arr[] = {'x', 'u', 'a', 'n', '\0'};
81
    cout << "char_arr = " << char_arr << endl;
82
    cout << "*char_arr = " << *char_arr << endl;
83
    cout << "&char_arr = " << &char_arr << endl;
84
    cout << "&(*char_arr) = " << &(*char_arr) << endl;
85
    cout << "??? Why char_arr is printed out completely, why arr only print number 10???";
86
    cout << endl << endl;
87

  
88
    cout << "### ['x', 'u', 'a', 'n']" << endl;
89
    char char_arr_2[] = {'x', 'u', 'a', 'n', '\0'};
90
    cout << "char_arr = " << char_arr_2 << endl;
91
    cout << "*char_arr = " << *char_arr_2 << endl;
92
    cout << "&(*char_arr) = " << &(*char_arr_2) << endl;
93

  
94
    func_reference(x);
95
    func_reference(*arr);
96
}
fiddle/template_function.cpp
1
/*
2
Testing the template function
3
*/
4
#include <iostream>
5
using namespace std;
6

  
7

  
8
template <class T1, class T2>
9
class custom_pair {
10
public:
11
    T1 first;
12
    T2 second;
13

  
14
    custom_pair(const T1& x, const T2& y) : first(x), second(y) // member initialization list
15
    {    }
16

  
17
};
18

  
19
template <class T1, class T2>
20
bool operator<(const custom_pair<T1, T2>& x, const custom_pair<T1, T2>& y)
21
{
22
    return x.first < y.first;
23
}
24

  
25
template <class T1, class T2>
26
std::ostream& operator<< (std::ostream & out, const custom_pair<T1, T2> & data) {
27
    out << "pair: " << data.first << " - " << data.second << endl;
28
    return out ;
29
}
30

  
31
template <class T>
32
const T& custom_max(const T& x, const T& y)
33
{
34
    return x == y ? y : x;
35
}
36

  
37
int main()
38
{
39
    int u = 3, v = 5;
40
    double d = 4.7;
41
    custom_pair<double, long> pair1(3.14, 999);
42
    custom_pair<double, long> pair2(1.6, 999);
43

  
44
    cout << custom_max(u, v) << endl;
45
    cout << custom_max(d, 9.3) << endl;
46
    cout << custom_max(pair1, pair2) << endl;
47
    // cout << custom_max(u, d) << endl; // ERROR: deduced conflicting types for parameter ‘T’ (‘int’ and ‘double’)
48

  
49
}

Also available in: Unified diff