forked from JuezUN/opt-cpp-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-function-types.cpp
More file actions
92 lines (68 loc) · 1.73 KB
/
Copy pathcpp-function-types.cpp
File metadata and controls
92 lines (68 loc) · 1.73 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
// From the test suite of https://github.com/codespecs/daikon
// daikon/tests/kvasir-tests/
int overloaded_foo(int a, int b);
int overloaded_foo(double a, double b);
int overloaded_foo(char* a, char* b);
int overloaded_foo(int a);
void pass_by_reference(int &ref_a, int val_b);
void pass_ptr_by_reference(int* &ref_a);
void default_args_func(int a, int b, int c);
int& return_reference(int &a);
// Tests the following C++ features:
// 1. Function overloading
// 2. Pass parameters by reference
// 3. Default values for function arguments
// 4. 'const' modifiers
int globalA = 1000;
int globalB = 2000;
int overloaded_foo(int a, int b) {
return a + b;
}
int overloaded_foo(double a, double b) {
return (int)(a + b);
}
int overloaded_foo(char* a, char* b) {
return (int)(a[0] + b[0]);
}
int overloaded_foo(int a) {
return 42;
}
void pass_by_reference(int &ref_a, int val_b) {
ref_a++;
val_b++;
}
void pass_ptr_by_reference(int* &ref_a) {
ref_a = &globalB;
}
int& return_reference(int &a) {
a = -999;
return a;
}
void pass_by_const_reference(const int& cref_a) {
}
void default_args_func(int a, int b = 5, int c = 10) {
}
int main() {
int i_x = 1, i_y = 2;
double d_x = 1.56, d_y = 2.15;
char *str_x = (char*)"hello", *str_y = (char*)"world";
overloaded_foo(i_x, i_y);
overloaded_foo(d_x, d_y);
overloaded_foo(str_x, str_y);
overloaded_foo(100);
int a = 10;
int b = 10;
int* ref_a = &globalA;
pass_by_reference(a, b);
pass_by_reference(a, b);
pass_by_reference(a, b);
pass_by_reference(a, b);
pass_by_reference(a, b);
default_args_func(1);
default_args_func(1, 2);
default_args_func(1, 2, 3);
pass_ptr_by_reference(ref_a);
pass_by_const_reference(a);
return_reference(a);
return 0;
}