Function overloading in C ++
I. the parameter table of the function is strictly matched. A null parameter indicates that no parameter exists. The void parameter can still be used. the implicit declaration method in C language is no longer supported. You must declare or define the method before calling a function. the int type returned by the function cannot be omitted. function can be reloaded. 1. in the same scope, function names are the same, and different functions in the parameter list may exist. The return value is any 2. when a function is overloaded, using a function pointer may be a problem. When assigning a value to a function pointer, the pointer type represents a specific function. 3. function Overloading is implemented based on the function name during compilation. 4. when a program calls a overloaded function across the compiler or other languages, the function name cannot be changed during compilation; otherwise, the function cannot be called. extern "C" can tell the compiler to compile and process it like the C language.
/// Main. cpp // day01-Function /// Created by aiqiyisheng on 15/1/11. // Copyright (c) 2015 HYR. All rights reserved. // # include
Using namespace std; // if the return value type is not specified in the C language, the int type/** fa () is returned by default. // when declaring, the parameter can be left blank, the parameter fa (int a, int B) can be arbitrarily given during definition) {// define // function body} ** // the return value type must be written to the function in C ++. If no return value exists, if you do not specify a parameter in the return value C ++ of void, by default, you will be given a void null parameter * // The int addExp (int heroExp, int addExp) {return heroExp + addExp;} added to the experience function after hitting the hero ;} // experience int addExp2 (int heroExp, double addExpPtr) {return heroExp * (1 + addExpPtr);}/* void addExp2 (int heroExp, double addExpPtr) {// The reload has nothing to do with the return value type} * // The method reload * The method name is the same, the parameter list is different (number of parameters, parameter type, Parameter order) in this way, the method reloads the method with the same name when the compiler is compiling, And the compiler may generate a method name by itself, for example: int addExp (int heroExp, int addExp) may generate the method name int addExp_ I _ I ().... int addExp (int heroExp, double addExpPtr) may generate the method name int addExp_ I _d ()... during the call, the compiler will find the corresponding method based on the parameters you have given *** // rewrite: The subclass will overwrite the method of the parent class */int addExp (int heroExp, double addExpPtr) {return heroExp * (1 + addExpPtr);} // tell the compiler that the method is processed in the "C" language (it won't help you rename it) extern "C" void test () {cout <"extern \" C \ "function" <endl ;}// main function int main (int argc, const char * argv []) {int heroExp = 1000; cout <addExp (heroExp, 100) <endl; cout <addExp2 (heroExp, 0.5) <endl; cout <addExp (heroExp, 0.5) <endl; test (); return 0 ;}
The running result is as follows:
110015001500 role of extern "C" Program ended with exit code: 0
In addition, you should note that when you reload a function, what if you cannot find the matching type or find several matching types ???
# Include
Using namespace std; // defines the function overload // if no matching type is found, C ++ will find a relatively suitable type. // Similarly, if many suitable types are found, you do not know which one to execute, for example, comment out the int method // void fa (char ch) {cout <"fa (char ch)" <endl ;}// void fa (short st) {cout <"fa (short st)" <endl;} void fa (int it) {cout <"fa (int it)" <endl ;} void fa (long lg) {cout <"fa (long lg)" <endl;} void fa (float ft) {cout <"fa (float ft) "<endl;} void fa (double de) {cout <" fa (double de) "<endl;} int main () {// declare the variable char ch; short st; int it; long lg; float ft; double de; // fa (ch); fa (st); fa (it); fa (lg ); fa (ft); fa (de); return 0 ;}
The running result is as follows:
fa(int it)fa(int it)fa(int it)fa(long lg)fa(float ft)fa(double de)Program ended with exit code: 0