-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain01.cpp
More file actions
143 lines (124 loc) · 2.84 KB
/
Copy pathmain01.cpp
File metadata and controls
143 lines (124 loc) · 2.84 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
using std::cout, std::cin, std::endl;
const char *hf1_function_pointer(const char *s)
{
cout << s << endl;
return "world";
}
void function_pointer()
{
cout << endl
<< "test function_pointer" << endl;
const char *(*hf1)(const char *);
hf1 = hf1_function_pointer;
cout << hf1("hello") << endl;
}
void variable_reference()
{
cout << endl
<< "test variable_reference" << endl;
int n = 3;
int &r = n;
r = 5;
cout << "set r=5:: n: " << n << "; r: " << r << endl;
n = 7;
cout << "set n=7:: n: " << n << "; r: " << r << endl;
}
void hf1_swap_variable(int &a, int &b)
{
int tmp1;
tmp1 = a;
a = b;
b = tmp1;
}
void swap_variable()
{
cout << endl
<< "test swap_variable" << endl;
int a = 3;
int b = 5;
cout << "before swap:: a: " << a << "; b: " << b << endl;
hf1_swap_variable(a, b);
cout << "after swap:: a: " << a << "; b: " << b << endl;
}
void const_pointer()
{
cout << endl
<< "test const_pointer" << endl;
int a = 3;
int b = 5;
const int *p = &a;
cout << "*p=&a:: " << *p << endl;
p = &b;
cout << "*p=&b:: " << *p << endl;
}
void constant_variable()
{
const int MAX_VAL = 32;
const double PI = 3.14;
const char *SCHOOL_NAME = "Peking University";
}
void new_variable()
{
cout << endl
<< "test new_variable" << endl;
int *z1 = new int;
*z1 = 3;
cout << "*z1=3: " << *z1 << endl;
delete z1;
z1 = new int[2];
z1[0] = 1;
cout << "new int[4]: "
<< z1[0] << " "
<< z1[1] << " "
<< endl;
delete[] z1;
}
int hf1_overload(int a, int b)
{
cout << "hf1_overload(int, int): ";
if (a > b)
return a;
return b;
}
double hf1_overload(double a, double b)
{
cout << "hf1_overload(double, double): ";
if (a > b)
return a;
return b;
}
void function_overload()
{
cout << endl
<< "test function_overload" << endl;
cout << hf1_overload(3, 4) << endl;
cout << hf1_overload(3.0, 4.0) << endl;
}
int hf1_function_default_argument(int x1, int x2=0, int x3=0)
{
return x1 + x2 + x3;
}
void function_default_argument()
{
cout << endl
<< "test function_default_argument" << endl;
cout << "hf1(1): " << hf1_function_default_argument(1) << endl;
cout << "hf1(1,2): " << hf1_function_default_argument(1,2) << endl;
cout << "hf1(1,2,3): " << hf1_function_default_argument(1,2,3) << endl;
}
int main(int argc, char *argv[])
{
cout << "test argc / argv" << endl;
cout << "argc: " << argc << endl;
for (int i = 0; i < argc; i++)
cout << "argv: " << argv[i] << endl;
function_pointer();
variable_reference();
swap_variable();
const_pointer();
new_variable();
function_overload();
function_default_argument();
return 0;
}