C ++ function overloading and function Overloading
1. Concept of function Overloading
Function overloading means that a function can have the same name as other functions in the same scope, but the parameter types, number of parameters, return values, and function functions of these functions with the same name can be completely different.
I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!
2. Precautions for function Overloading
- Function overloading cannot be a function with different return values, at least in terms of the number, type, or sequence of parameters.
- The functions of the used overload functions should be the same. It is not a good programming style to enable the overload function to complete different functions, which will damage the readability of the program.
Example: define and test overload functions.
1 # include <iostream> 2 using namespace std; 3 4 int absolute (int x) 5 {6 return x <0? -X: x; 7} 8 9 double absolute (double x) 10 {11 return x <0? -X: x; 12} 13 14 int min (int x, int y) 15 {16 return x <y? X: y; 17} 18 19 int min (int x, int y, int z) 20 {21 return x <y? (X <z? X: z) :( y <z? Y: z); 22} 23 24 int main () 25 {26 int a = 3; 27 int B =-8; 28 int c = 6; 29 double d =-5.23; 30 cout <min (a, B) <endl; 31 cout <min (a, B, c) <endl; 32 cout <absolute (a) <endl; 33 cout <absolute (B) <endl; 34 cout <absolute (d) <endl; 35 return 0; 36} 37 38 // cainiao. I 'd like to ask for comments and comments, code writing habits and specifications, and so on !!! Thank you !!!
I am self-taught. The teaching materials may be a little old. If you have any questions, please correct me !!! Thank you !!!