function overloading in C + +

Source: Internet
Author: User

The parameter table of the function strictly matches, the null parameter means that there is no parameter, void parameter can still be usedtwo. Implicit declaration in C is no longer supported, and must be declared or defined before a function callthree. return type int of function cannot be omittedFour. Functions can be overloaded1. In the same scope, the function name is the same, the parameter list of different functions can exist several, its return value arbitrary2. When overloaded functions occur, using function pointers is a bit of a problem, and when assigning a value to a function pointer, the type of the pointer represents a specific function. 3. The implementation of function overloading is based on the compile-time change of the function name4. When a program crosses the compiler or calls an overloaded function by another language, the function name cannot be changed by the compiler at compile time, otherwise it cannot be called5. Extern "C" can tell the compiler to do the compilation, as in the C language.

main.cpp//day01-function////Created by Aiqiyisheng on 15/1/11.//Copyright (c) 2015 Hyr. All rights reserved.//#include <iostream>using namespace std;//do not write the return value type in the C language by default return integer int/* * FA ();//Declaration when the argument can not be written, When defining a parameter fa (int a,int b) {//define//function Body} * *//* function in C + + must write the return value type, if no return value, then write void the return value in C + + parameters If you do not write, you will default to a void null argument * The hero adds experience after the function int addexp (int heroexp,int addexp) {return heroexp + addexp;} Playing strange will increase the percentage of experience int addExp2 (int heroexp,double addexpptr) {return heroexp * (1 + addexpptr);} /*void addExp2 (int heroexp,double addexpptr) {//overload is independent of return value type} *//* * Method overload * Method name is the same, parameter list is different (number of arguments, parameter type, parameter order) Overloaded with a method that is unrelated to the return value type when the compiler encounters the same name at compile time, the compiler may generate a method name for itself, for example: int addexp (int heroexp,int addexp) may generate such a method name, int addexp_i_i ().    ..    int addexp (int heroexp,double addexpptr) may generate such a method name int addexp_i_d () ... At the time of the call, the compiler will find the corresponding method according to the parameters you give * * *//* rewrite: Subclass overriding method of parent class */int addexp (int heroexp,double addexpptr) {return heroexp * (1 + AD DEXPPTR);} Tells the compiler that the method is handled in the "C" language (Will not help you rename) extern "C" void Test () {cout << "extern \" C\ "<< Endl;}    Main function int main (int argc, const char * argv[]) {int heroexp = 1000;    cout << Addexp (heroexp, +) << Endl;        cout << addExp2 (heroexp, 0.5) << Endl;        cout << addexp (heroexp, 0.5) << Endl;    Test (); return 0;}

The results of the operation are as follows:
110015001500extern "C" function program ended with exit code:0



It is also important to note that when a function is overloaded, it is important to note that if a match type is not found, or if several matching types are found, what should I do???
#include <iostream>using namespace std;//define function   function overloading//If no matching type is found, C + + will find a relatively suitable type//also  if a lot of suitable types  are found That's not the way to do that one, such as commenting 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 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 results of the operation are 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












function overloading in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.