zoukankan      html  css  js  c++  java
  • part5.(one)函数重载

    一、普通函数重载

    C语言中一个函数只能处理一个类型的数据。C++中一个函数可以处理多个数据。

    普通函数重载
    #include<iostream.h>
    #include<string.h>
    int max(int x,int y){
    
        return x>y?x:y;
    }
    
    double max(double x,double y){
    
      return x>y?x:y;
    }
    
    char* max(char* x,char* y){
    
        return strcmp(x,y)>0?x:y;
    }
    
    void main(){
       cout<<max(25,16)<<endl;
       cout<<max(25.10,14.32)<<endl;
       cout<<max("Windows","Android")<<endl;
    
    }

    需要注意一点,即在使用重载的普通函数时,应注意避免如下所示的二义性。

    错误的代码示例
     1  // difference only in return type is not enough
     2  #include <iostream.h>
     3  
     4  int fun(int y)
     5  {
     6      return y;
     7  }
     8  
     9  void fun(int y)
    10  {
    11      ++y;
    12  }
    13  // error: 'void fun(int)' : overloaded function differs
    14  // only by return type from 'int fun(int)'
    15  // error: 'fun' : redefinition; different basic types
    16  void main()
    17  {
    18      int i=5;
    19      cout<<fun(i)<<endl;
    20  }

    二、构造函数重载

  • 相关阅读:
    jQuery Ajax 实例 全解析
    用Javascript评估用户输入密码的强度
    常用网址
    常用的107条Javascript
    根据键盘操作表格
    HTML5吧
    css3动画简介以及动画库animate.css的使用
    jquery插件下载地址
    CEO、COO、CFO、CTO
    springboot与shiro配置
  • 原文地址:https://www.cnblogs.com/kalo1111/p/3069995.html
Copyright © 2011-2022 走看看