-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions_advance_002.hpp
419 lines (348 loc) · 12.5 KB
/
functions_advance_002.hpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#ifndef __FUNCTIONS_ADVANCE_002_H
#define __FUNCTIONS_ADVANCE_002_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <exception>
#include <limits>
#include <random>
#include <typeinfo>
#include <tuple>
#include <regex>
#include <memory>
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <chrono>
#include <filesystem>
#include <chrono>
#include <optional>
#include <cassert>
#include <cstddef>
#include <cwctype>
#include <ctime>
#include <cstdlib>
#include <cstdint>
#include <climits>
#include <cctype>
#include <cstring>
#include <cmath>
using namespace std;
/*
- Function Overload:
- Function overloading is a powerful feature in C++ that allows you to define multiple functions with the same name in the same scope, but with different parameter lists.
- This means you can have functions that perform similar tasks but operate on different types or different numbers of parameters.
- The compiler resolves which overloaded function to call based on the arguments provided during the function call.
- Function overloading allows you to write more expressive and readable code, providing different function implementations based on the input types, without having to invent distinct function names for each variation.
- Here are some key points to consider when using function overloading in C++:
- Function Signatures:
- Function overloading is based on function signatures, which consist of the function name and the types of its parameters.
- The return type is not considered in function overloading.
- Parameter Types and Order:
- The parameters of the overloaded functions must differ in either their types or the number of parameters.
- The order of parameters can also be used to differentiate overloaded functions.
- Return Type:
- As mentioned earlier, the return type alone does not distinguish overloaded functions.
- Two functions with the same name and the same parameter list but different return types are not considered overloaded; it will result in a compilation error.
- Scope:
- Overloading can happen within the same scope, including global scope, class scope, or namespace scope.
- Default Arguments:
- Functions with default arguments can be overloaded, but be careful with the order of overloaded functions with default arguments, as it can lead to ambiguous function calls.
- Best Match Resolution:
- During a function call, the compiler tries to find the best match for the provided arguments.
- If an exact match is found, that function is called.
- If no exact match is found, the compiler tries to find the closest match that can be converted implicitly.
- Function Overloading vs Function Templates:
- Function templates provide another way to achieve similar behavior as function overloading.
- Templates allow you to write generic functions that can operate on multiple types.
- Function overloading, on the other hand, is more suitable when you have different implementations based on specific types.
*/
// Function to add integers
int fn_add(int a, int b)
{
return a + b;
}
// Function to add doubles
double fn_add(double a, double b)
{
return a + b;
}
void func_add_overloaded()
{
int result_1 = fn_add(3, 4); // Calls the int add(int, int)
double result_2 = fn_add(2.5, 3.7); // Calls the double add(double, double)
std::cout << "Result 1: " << result_1 << std::endl;
std::cout << "Result 2: " << result_2 << std::endl;
}
/*
- Overloading with different parameters:
*/
// Function to perform addition
int fn_calculate(int a, int b)
{
return a + b;
}
// Function to perform subtraction
int fn_calculate(int a, int b, int c)
{
return a - b - c;
}
// Function to perform multiplication
int fn_calculate(int a, int b, int c, int d)
{
return a * b * c * d;
}
// Function to perform division
double fn_calculate(double a, double b)
{
if (b != 0)
return a / b;
else
{
std::cout << "Error: Division by zero!" << std::endl;
return 0;
}
}
void func_calculate_overloaded()
{
int sum = fn_calculate(1, 2); // Calls calculate(int a, int b)
int difference = fn_calculate(10, 2, 3); // Calls calculate(int a, int b, int c)
int product = fn_calculate(2, 3, 4, 5); // Calls calculate(int a, int b, int c, int d)
double division = fn_calculate(10.0, 2.0); // Calls calculate(double a, double b)
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Division: " << division << std::endl;
}
/*
- Overloading with pointer parameters:
*/
// Function to print an integer value
void fn_print_int_value(int* ptr)
{
if (ptr)
{
std::cout << "Integer value: " << *ptr << std::endl;
}
else
{
std::cout << "Invalid pointer!" << std::endl;
}
}
// Function to print a double value
void fn_print_double_value(double* ptr)
{
if (ptr)
{
std::cout << "Double value: " << *ptr << std::endl;
}
else
{
std::cout << "Invalid pointer!" << std::endl;
}
}
// Function to swap two integer values
void fn_swap_int_values(int* ptr1, int* ptr2)
{
if (ptr1 && ptr2)
{
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
else
{
std::cout << "Invalid pointers for swapping!" << std::endl;
}
}
void func_print_overloaded_values()
{
int num1 = 5;
int num2 = 10;
double dbl1 = 3.14;
fn_print_int_value(&num1); // Calls fn_print_int_value(int*)
fn_print_double_value(&dbl1); // Calls fn_print_double_value(double*)
std::cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
fn_swap_int_values(&num1, &num2); // Calls fn_swap_int_values(int*, int*)
std::cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
}
/*
- Overloading with reference parameters:
*/
// Function to calculate the area of a circle
double fn_calculate_area_with_ref(const double& radius)
{
return M_PI * radius * radius;
}
// Function to calculate the area of a rectangle
double fn_calculate_area_with_ref(const double& length, const double& width)
{
return length * width;
}
// Function to calculate the area of a triangle
double fn_calculate_area_with_ref(const long double& base, const double& height)
{
return 0.5 * base * height;
}
void func_calculate_overloaded_area()
{
double circle_radius = 2.5;
double rectangle_length = 3.0;
double rectangle_width = 4.0;
double triangle_base = 5.0;
double triangle_height = 2.0;
double circle_area = fn_calculate_area_with_ref(circle_radius); // Calls fn_calculate_area(const double& radius)
double rectangle_area = fn_calculate_area_with_ref(rectangle_length, rectangle_width); // Calls fn_calculate_area(const double& length, const double& width)
double triangle_area = fn_calculate_area_with_ref(triangle_base, triangle_height); // Calls fn_calculate_area(const double& base, const double& height)
std::cout << "Area of the circle: " << circle_area << std::endl;
std::cout << "Area of the rectangle: " << rectangle_area << std::endl;
std::cout << "Area of the triangle: " << triangle_area << std::endl;
}
// Function to calculate the sum of integers in an array
int fn_calculate_sum(const int arr[], const int& size)
{
int sum = 0;
for (int i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum;
}
// Function to calculate the sum of doubles in an array
double fn_calculate_sum(const double arr[], const int& size)
{
double sum = 0;
for (int i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum;
}
void func_calc_overloaded_sum()
{
int int_array[] = {1, 2, 3, 4, 5};
double double_array[] = {1.1, 2.2, 3.3, 4.4, 5.5};
int int_sum = fn_calculate_sum(int_array, 5); // Calls fn_calculate_sum(const int arr[], const int& size)
double double_sum = fn_calculate_sum(double_array, 5); // Calls fn_calculate_sum(const double arr[], const int& size)
std::cout << "Sum of integers: " << int_sum << std::endl;
std::cout << "Sum of doubles: " << double_sum << std::endl;
}
/*
- Overloading with const parameters by value:
*/
// Function to find the minimum value among integers
int fn_find_min(const int a, const int b)
{
return (a < b) ? a : b;
}
// Function to find the minimum value among doubles
double fn_find_min(const double a, const double b)
{
return (a < b) ? a : b;
}
void func_find_min_overloaded()
{
int int_min = fn_find_min(5, 10); // Calls fn_find_min(const int a, const int b)
double double_min = fn_find_min(3.14, 2.71); // Calls fn_find_min(const double a, const double b)
std::cout << "Minimum of integers: " << int_min << std::endl;
std::cout << "Minimum of doubles: " << double_min << std::endl;
}
/*
- Overloading with const pointer and pointer to const parameters:
*/
// Function to swap two integer values using const pointer parameters
void fn_swap_values(int* const ptr1, int* const ptr2)
{
if (ptr1 && ptr2)
{
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
else
{
std::cout << "Invalid pointers for swapping!" << std::endl;
}
}
// Function to swap two double values using pointer to const parameters
void fn_swap_values(const double* ptr1, const double* ptr2)
{
if (ptr1 && ptr2)
{
double temp = *ptr1;
// Note: We can't modify the values since pointers are const.
// *ptr1 = *ptr2; // This would cause a compilation error.
// *ptr2 = temp;
}
else
{
std::cout << "Invalid pointers for swapping!" << std::endl;
}
}
void func_swap_values_with_overloaded_ptr_to_const_const_ptr()
{
int num1 = 5;
int num2 = 10;
double dbl1 = 3.14;
double dbl2 = 2.71;
int* ptr1 = &num1;
int* ptr2 = &num2;
const double* dbl_ptr1 = &dbl1;
const double* dbl_ptr2 = &dbl2;
fn_swap_values(ptr1, ptr2); // Calls fn_swap_values(int* const ptr1, int* const ptr2)
fn_swap_values(dbl_ptr1, dbl_ptr2); // Calls fn_swap_values(const double* ptr1, const double* ptr2)
std::cout << "After swapping integers: num1 = " << num1 << ", num2 = " << num2 << std::endl;
std::cout << "After swapping doubles: dbl1 = " << dbl1 << ", dbl2 = " << dbl2 << std::endl;
}
/*
- Overloading with default parameters:
*/
const double PI = 3.14159265359;
// Function to calculate the volume of a sphere
double fn_calculate_volume(const long double& radius, const double& pi = PI)
{
return 4.0 / 3.0 * pi * pow(radius, 3);
}
// Function to calculate the volume of a cube
double fn_calculate_volume(const double& side)
{
return pow(side, 3);
}
// Function to calculate the volume of a rectangular prism
double fn_calculate_volume(const double& length, const double& width, const double& height)
{
return length * width * height;
}
void func_calculate_volume_with_default_parameter_overloaded()
{
double sphere_radius = 2.5;
double cube_side = 3.0;
double prism_length = 4.0;
double prism_width = 5.0;
double prism_height = 2.0;
double sphere_volume = fn_calculate_volume(sphere_radius); // Calls fn_calculate_volume(const double& radius, const double& pi = PI)
double cube_volume = fn_calculate_volume(cube_side); // Calls fn_calculate_volume(const double& side)
double prism_volume = fn_calculate_volume(prism_length, prism_width, prism_height); // Calls fn_calculate_volume(const double& length, const double& width, const double& height)
std::cout << "Volume of the sphere: " << sphere_volume << std::endl;
std::cout << "Volume of the cube: " << cube_volume << std::endl;
std::cout << "Volume of the rectangular prism: " << prism_volume << std::endl;
}
#endif